Lead generation from Custom Contact Form

hi,

I’ve been looking a lot for a solution to my problem. I’ve got a Wordpress site with a Plugin for the Contact Form.
I want to send the information submitted from the customers to the Lead in the CRM.

I know about the Web-to-lead module, but my plugin has a lot more features, so I would not change it.

I’ve read about the use of REST api, and i’ve created a php file like this one: CODE
How could I initialize the file once the form has been submitted?

Thanks!

I have done something like this in the past with this plugin for a home grown CRM. Edit your form in WordPress and go to the Email tab and select Advanced.

Under the Advanced section you will see “Javascript Events”. You can use Ajax to call your Rest API file from this trigger. I have included what I have used in the past. Obviously the form fields will be different, but it should point you in the right direction.


$.ajax ({
	url: "<URL TO YOUR REST API>",
	type: "POST",
	data: {
		first_name: $('#pwebcontact1_field-name').val(),
		last_name: $('#pwebcontact1_field-lastname').val(),
		email: $('#pwebcontact1_field-email').val(),
		home_phone: $('#pwebcontact1_field-phone').val(),
		street_address: $('#pwebcontact1_field-streetaddress').val(),
		city: $('#pwebcontact1_field-city').val(),
		state: $('#pwebcontact1_field-state').val(),
		postal_code: $('#pwebcontact1_field-zipcode').val(),
		//country: "US",
		requests: $('#pwebcontact1_field-request').val(),
		message: $('#pwebcontact1_field-message').val()
	},
	dataType: "json",
	success: function (result) {
       
 },
    });
1 Like

Thanks! That helped me a lot, Now it works!

Now I’ve got just one little problem to solve. Thanks to your code, I can trigger the REST API and there is the creation of a new lead, so I’ve achieved my goal.
But the Chrome Console put in evidence a 500 (Internal Server Error), coming from my php file.

Here there is my php:

<?php

// specify the REST web service to interact with

$url = '***url***/service/v4_1/rest.php';


// Open a curl session for making the call
$curl = curl_init($url);

// Tell curl to use HTTP POST
curl_setopt($curl, CURLOPT_POST, true);

// Tell curl not to return headers, but do return the response
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);



// Set the POST arguments to pass to the Sugar server

$parameters = array(
    'user_auth' => array(
    	'user_name' => '**user**',
    	'password' => md5('**password**'),
        ),
    );

$json = json_encode($parameters);
$postArgs = array(
    'method' => 'login',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => $json,
    );
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);

// Make the REST call, returning the result
$response = curl_exec($curl);
if (!$response) {
    die("Connection Failure.\n");
}

// Convert the result from JSON format to a PHP array
$result = json_decode($response);
if ( !is_object($result) ) {
    die("Error handling result.\n");
}
if ( !isset($result->id) ) {
    die("Error: {$result->name} - {$result->description}\n.");
}

// Get the session id
$session = $result->id;

$parameters = array(
    'session' => $session, //Session ID
    'module' => 'Leads',  //Module name
    'name_value_list' => array ( 
            		array('name'=>'salutation','value'=>'Mr.'),
			array('name'=>'first_name','value'=>$_POST['first_name']),
			array('name'=>'last_name','value'=>$_POST['last_name']),
			array('name'=>'status', 'value'=>'New'),
			array('name'=>'phone_work', 'value'=>$_POST['phone_work']),
			array('name'=>'email1', 'value'=>$_POST['email1']),
			array('name'=>'account_name','value'=>$_POST['account_name']),
			array('name'=>'lead_source','value'=>'Web Site'),
			array('name'=>'description','value'=>$_POST['description'])
			        ), 
    ); 
$json = json_encode($parameters);
$postArgs = array(
                'method' => 'set_entry',
                'input_type' => 'JSON',
                'response_type' => 'JSON',
                'rest_data' => $json
                );
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);
if (!$response) {
    die("Connection Failure.\n");
}

// Convert the result from JSON format to a PHP array
$result = json_decode($response);
if ( !is_object($result) ) {
    die("Error handling result.\n");
}
if ( !isset($result->id) ) {
    die("Error: {$result->name} - {$result->description}\n.");
}

// Get the newly created record id
$recordId = $result['id'];

print "New Record Created with ID ".$recordId;

Nevermind, I solved changing a little the code.

If someone else is looking for this, here is the working code:

<?php

    $url = "**url**/service/v4_1/rest.php";
    $username = "admin";
    $password = "password";

    //function to make cURL request
    function call($method, $parameters, $url)
    {
        ob_start();
        $curl_request = curl_init();

        curl_setopt($curl_request, CURLOPT_URL, $url);
        curl_setopt($curl_request, CURLOPT_POST, 1);
        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl_request, CURLOPT_HEADER, 1);
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

        $jsonEncodedData = json_encode($parameters);

        $post = array(
             "method" => $method,
             "input_type" => "JSON",
             "response_type" => "JSON",
             "rest_data" => $jsonEncodedData
        );

        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
        $result = curl_exec($curl_request);
        curl_close($curl_request);

        $result = explode("\r\n\r\n", $result, 2);
        $response = json_decode($result[1]);
        ob_end_flush();

        return $response;
    }

    //login ---------------------------------------------     
	$login_parameters = array(
         "user_auth" => array(
              "user_name" => $username,
              "password" => md5($password),
              "version" => "1"
         ),
         "application_name" => "RestTest",
         "name_value_list" => array(),
    );

    $login_result = call("login", $login_parameters, $url);

    /*
    echo "<pre>";
    print_r($login_result);
    echo "</pre>";
    */

    //get session id
    $session_id = $login_result->id;

    //create account -------------------------------------     
	$set_entry_parameters = array(
         //session id
         "session" => $session_id,

         //The name of the module from which to retrieve records.
         "module_name" => "Leads",

         //Record attributes
         "name_value_list" => array(
            array('name'=>'salutation','value'=>'Mr.'),
			array('name'=>'first_name','value'=>$_POST['first_name']),
			array('name'=>'last_name','value'=>$_POST['last_name']),
			array('name'=>'status', 'value'=>'New'),
			array('name'=>'phone_work', 'value'=>$_POST['phone_work']),
			array('name'=>'email1', 'value'=>$_POST['email1']),
			array('name'=>'account_name','value'=>$_POST['account_name']),
			array('name'=>'lead_source','value'=>'Web Site'),
			array('name'=>'description','value'=>$_POST['description'])
         ),
    );

    $set_entry_result = call("set_entry", $set_entry_parameters, $url);

    echo "<pre>";
    print_r($set_entry_result);
    echo "</pre>";

?>