Salesforce Web-to-Lead form integration using PHP and cURL

Using this method, we can achieve the following scenarios:

1) Security Advantage of this process is hiding your Salesforce organisation ID

2) We can add multiple business requirements before/after submitting Leads data to SFDC.

Step 1:

Generate Salesforce web-to-lead form

<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <META> element to your page <head>.      -->
<!--  If necessary, please modify the charset parameter to specify the        -->
<!--  character set of your HTML page.                                        -->
<!--  ----------------------------------------------------------------------  -->

<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />

<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following
<FORM> element to your page.             -->
<!--  ----------------------------------------------------------------------  -->

<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST"><input type="hidden" name="oid" value="00D90000000Aiji" />
<input type="hidden" name="retURL" value="http://www.theblogreaders.com" /><!--  ----------------------------------------------------------------------  -->
<!--  NOTE: These fields are optional debugging elements. Please uncomment    -->
<!--  these lines if you wish to test in debug mode.                          -->
<!--  <input type="hidden" name="debug" value=1>                              -->
<!--  <input type="hidden" name="debugEmail"                                  -->
<!--  value="info@theblogreaders.com">                                   -->
<!--  ----------------------------------------------------------------------  -->

<label for="first_name">First Name</label><input id="first_name" type="text" maxlength="40" name="first_name" size="20" />

<label for="last_name">Last Name</label><input id="last_name" type="text" maxlength="80" name="last_name" size="20" />

<label for="email">Email</label><input id="email" type="text" maxlength="80" name="email" size="20" />

<label for="company">Company</label><input id="company" type="text" maxlength="40" name="company" size="20" />

<label for="URL">Website</label><input id="URL" type="text" maxlength="80" name="URL" size="20" />

<label for="phone">Phone</label><input id="phone" type="text" maxlength="40" name="phone" size="20" />

<input type="submit" name="submit" />

</form>

STEP2:

Modify the above Form Action like below


<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

<form action="application.php" method="POST">

STEP3:

Create application.php file and upload it in your server (using FTP credentials)


<?PHP

if(isset($_POST['submit'])) {

$oid = "YOUR SALESFORCE ORG ID";
//create array of data to be posted
$post_data['oid'] = $oid;
$post_data['first_name'] = trim($_POST['first_name']);
$post_data['last_name'] = trim($_POST['last_name']);
$post_data['email'] = trim($_POST['email']);
$post_data['company'] = trim($_POST['company']);
$post_data['retURL'] = 'https://www.theblogreaders.com';
$post_data['00N90000009SXUs'] = trim($_POST['abn_acn']);   // CUSTOM FIELDS IN LEAD OBJECT

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//print_r($post_data);
//create cURL connection
$curl_connection = curl_init('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
//print_r(curl_getinfo($curl_connection));
//echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);
//close the connection
curl_close($curl_connection);

// HERE YOU CAN ADD ANY BUSINESS REQUIREMENT,

//FOR EXAMPLE:

//1) INSERT THE LEADS DATA TO YOUR MYSQL DATABASE

//2) SEND THE EMAIL, etc

}

?>

<form action="application.php" method="POST">
<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>

<label for="last_name">Last Name</label><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>

<label for="email">Email</label><input  id="email" maxlength="80" name="email" size="20" type="text" /><br>

<label for="company">Company</label><input  id="company" maxlength="40" name="company" size="20" type="text" /><br>

<label for="URL">Website</label><input  id="URL" maxlength="80" name="URL" size="20" type="text" /><br>

<label for="phone">Phone</label><input  id="phone" maxlength="40" name="phone" size="20" type="text" /><br>
<input type="submit" name="submit">

</form>

STEP4:

once modifed as per above like creating a new php file called application.php, then upload it in your server and run the file in any of the browser and fill the lead details using the newly created form.

then log into your SalesForce Account and click the “Leads” tab, then change the view to Today’s Leads and press Click Go!

 

(2493)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 10 Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply