Recently our company began a trial of Salesforce.com. In doing so we opted not to layout the cash for the program that gives you full API access (at the moment any ways). I needed a way to push leads into the system. They have a standard web-to-lead process that requires a form submission and then post back to return URL. I already had a lot of forms that I was using to capture data. Instead of reworking all my forms, I just created a way to post to the Salesforce web-to-lead form from the server side.
I started by creating a basic function which passes in the data I want to capture and pushes it to the Sales force form.
function add_to_salesforce($source, $name, $email, $company, $city, $state, $zip, $phone, $description, $street = "")
{
// simple way of breaking apart the name
$names = split(" ", $name);
//set POST variables
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$fields = array(
'last_name'=>urlencode($names[1]),
'first_name'=>urlencode($names[0]),
'street'=>urlencode($street),
'city'=>urlencode($city),
'state'=>urlencode($state),
'zip'=>urlencode($zip),
'company'=>urlencode($company),
'description'=>urlencode($description),
'email'=>urlencode($email),
'phone'=>urlencode($phone),
'mycustomefieldid' => urlencode($source), // custom field
'oid' => 'youridgoeshere', // insert with your id
'retURL' => urlencode('http://www.yourreturnurl.com'), // sending this just in case
'debug' => '1',
'debugEmail' => urlencode("youremail@youremail.com"), // your debugging email
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
Once that was setup all I had to do was call the function (which I wrapped in a class with something like this:
Works like a charm so far. If you’ve got a slicker way of perfoming this operation, let me know, I’d love to learn how you did it.