sim.plified.com

Chris Pollock

Chris Pollock - web developer & ecommerce entrepreneur
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.

  • http://phpandsalesforce.com phpandsfdc

    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/

  • Pingback: Lead Generation via the Salesforce.com API vs. Web-to-Lead | PHP and Salesforce

  • http://www.digrai.es Pedro Jose

    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.

  • 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

  • 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

  • http://web.simplifiedbuilding.com cwpollock

    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.

  • http://www.janova.us Blake

    Is there anyway to customize the fields which are populated in salesforce? I have a select list that asks the customer where they heard about us. I want this to auto fill out my custom field "Web Source" in the sales force new lead that gets created. How can I do this within this function you posted?

  • http://web.simplifiedbuilding.com cwpollock

    @Blake

    You need to create the fields you want to populate in sales force and then when you walk through their form creation wizard it will give you and ID for that custom field. You can then get the information out of the post and associate it with that sales force filed id just like I do in the example above.

  • MSeely

    Thank you so much for this post. I was really frustrated with having so little control over the salesforce web-to-lead form, especially being forced to create a return URL. With a little tweak, I was able to get our PHP contact form to work as-is by inserting your code. Much appreciated!

  • http://www.johnboy.com John

    Thanks a lot for sharing this. It works great for me.

  • http://theblogreaders.com Gomathi

    HI,
    You can find the Basic Tutorials about PHP and Salesforce Implementation using Below URL
    http://theblogreaders.com/integration-between-salesforce-and-php

    TheBlogreaders.com
    Blog | Salesforce Certified Administrator | Salesforce Certified Developer

  • Sam

    Thanks – this was helpful. I suggest adding the class tag around your code in the demonstration to help novices.

  • uma devi

    hi,
    I create the simple form for account page with firstname,lastname,email,mobile,city only. How to insert the value into account

Things I see

#gocubs. I’ll root for them over the Dodgers :)PhotoBig stuff planned here tomorrow night. #alcsIt’s hot in these here parts.Hunting is one of the few activities that necessitates slowing down and becoming keenly aware of your surroundings.Magic hour #nofilterHunting time. #youthhuntWarming up his swing. GO #southsidelittleleague !!My first ever granny square! Why when I tried this years ago could I not figure it out? Probably YouTube tutorials dawned since then! Like a free craft class at 9:30 at night --- hard to beat it:blush:The house is set for next weekend. #youthhunt Mr SmilesHappy birthday facesNo wonder Lucas has been eating like a horse lately... he's growing like a weed!!! #lifewithboys #growthspurt #threeyearoldsarefunDecided to try and remember how to crochet. Top is my first attempt at a square :joy:. Oops!!! #droppingstitches #crochetingisfunHanging out w mom and Julie in the finger lakes for the weekend. Had a fun time at Naples Grape festival. #lifewithgirls #umbrellahatsarecool #naplesgrapefest

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

#gocubs. I’ll root for them over the Dodgers :)PhotoBig stuff planned here tomorrow night. #alcsIt’s hot in these here parts.Hunting is one of the few activities that necessitates slowing down and becoming keenly aware of your surroundings.Magic hour #nofilterHunting time. #youthhuntWarming up his swing. GO #southsidelittleleague !!My first ever granny square! Why when I tried this years ago could I not figure it out? Probably YouTube tutorials dawned since then! Like a free craft class at 9:30 at night --- hard to beat it:blush:The house is set for next weekend. #youthhunt Mr SmilesHappy birthday facesNo wonder Lucas has been eating like a horse lately... he's growing like a weed!!! #lifewithboys #growthspurt #threeyearoldsarefunDecided to try and remember how to crochet. Top is my first attempt at a square :joy:. Oops!!! #droppingstitches #crochetingisfunHanging out w mom and Julie in the finger lakes for the weekend. Had a fun time at Naples Grape festival. #lifewithgirls #umbrellahatsarecool #naplesgrapefest