sim.plified.com

Chris Pollock

Chris Pollock - web developer (PHP/mySQL & ASP.NET)
undivided… my thoughts on world, family, church, business, technology and Jesus Christ (all in all)

Pushing Leads to Salesforce with PHP

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:

$myclass = new Myclass;
$myclass->add_to_salesforce("Contact Page", $name, $email_address, $company, "", "", "", $phone, $enquiry);
      

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.

6 Comments, Comment or Ping

  1. Chris,

    Great post! It's not necessarily apparent that both the client and server can send the POST request to Salesforce, which when sent from the server also allows you to send only the information you need in the lead, and avoid sending users' passwords, etc… If you're interested, I wrote up a little post on the differences between using Web-to-Lead and using the API, the address is http://phpandsalesforce.com/2009/06/21/lead-generation-via-the-salesforce-com-api-vs-web-to-lead/

  2. Hello friend, I belong to a Spanish company and so far Salesforce giving me many problems.

    I have a question for you, guaranteed way you enter data in Salesforce that you see the origin of the candidate (eg, google adwords, direct web, ..)

    What do you mean, 'oid' => 'youridgoeshere', / / insert with your id … Will be assumed to know the id?

    I would be happy to answer because I have serious problems with the system.

  3. Nick

    Sorry for asking a dumb question! But how do you call this function?

    $myclass = new Myclass;$myclass->add_to_salesforce("Contact Page", $name, $email_address, $company, "", "",

    Would it be called when the html form is submitted? If so how would I do this?

    Thanks!

    Nick

  4. Ben

    Hi Chris,

    Thanks for the article - very useful.

    I've got this working on my website "contact us" page, but when I urlencode the SF field called "description" (which I've been asked to post the contact form's "comments" field into) it isn't getting un-encoded when it appears in Salesforce, so it ends up like this:

    This+is+a+test+%0D%0AUnfortunately+non-alphanumeric+characters+have+to+be+encoded+like+this%0D%0A%0D%0A%21%22%A3%24%25%5E%26%2A%28%29_%2B%0D%0A%0D%0A-%3D%7B%7D%5B%5D%3A%40%7E%3B%27%23%3C%3E%3F%2C.%2F%AC%60%60%A6

    Any ideas how to avoid this?

    Many thanks!

    Ben

  5. Hmm.. can't say that it's doing that for me. Are you doing a GET instead of POST form request. That could be the issue. If not, then try doing a PHP urldecode on the posted var before you push it over to salesforce.

Reply to “Pushing Leads to Salesforce with PHP”

Things I see

PVC TentPVC TentPVC TentSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland Park

Chris Pollock

Web Developer - proficient in both PHP and ASP.NET.
Rochester, New York

View my web developement site.

View Chris Pollock's LinkedIn profile

My Pictures

PVC TentPVC TentPVC TentSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland ParkSledding at Highland Park