+ Start a Discussion

[PHP] cURL problems; web-to-lead

How can I have PHP send data from a web form to salesforce.com?  I am trying to ultimately have the user fill out the form, hit the submit button, validate the form then send the data to salesforce.com (if the form is valid) without refreshing the page.  I am using cURL to do the PHP work. 


The code below is in isolation.  This should work by itself, correct?

* the 'oid' value is fake.


 $data = array(
  'first_name'=> 'Marc',
  'last_name' => 'Kass',
  'email'  => 'marckass@example.com',
  'submit' => 'true',
  'oid'  => '11X011111111xX1'
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "http://www.salesforce.com/servlet/servlet.WebToLead–encoding=ISO-8859-1");
 curl_setopt($ch, CURLOPT_POST, 1 );
 curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $postResult = curl_exec($ch);

Any comments?
Any suggestions?


Check out my blog post about PHP scripts I wrote that are designed to capture data from a web to lead form, process it (in this case it asks the Akismet anti-spam service if it's spam) and then send it onto Salesforce web-to-lead using cURL.

That blog post has PHP files you can download that should "just work".

You can learn a bit more about it by watching the Dreamforce video from the PHP Session.  Once you get to that link, click to view the session materials.  My part is the second part.

In short, the following is how I do it:

  • I use the $_POST variable and spin through it and put its contents into a local array
  • I ask Akismet if it's spam and append a yes/no response onto the array
  • I send it to Salesforce via cURL.  Below is the code.  2 differences from your's:
    • You don't need to RETURNTRANSFER line.  If you send it the retURL value, the response from Salesforce will redirect the user to another page.
    • My code calls the http_build_query function when setting up the POSTFIELDS.
 * Use cURL to post to Salesforce web to lead 
 // Create a new cURL resource
 $ch = curl_init(); 

 // Set Options
  // Point to the Salesforce Web to Lead page
  curl_setopt($ch, CURLOPT_URL, "https://www.salesforce.com/servlet/servlet.WebToLead—encoding=UTF-8");
  // Set the method to POST
  curl_setopt($ch, CURLOPT_POST, 1);
  // Pass POST data
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
 curl_exec($ch); // Post to Salesforce



Ah…thank you so much for your comment!  I have been seriously working on this for about 3 weeks and haven’t accomplished much.  I will read your blog article(s) tomorrow and will definitely post my status.

Message Edited by marc_____ on 12-21-2007 04:56 PM

Ok, I tried eliminating the Akismet code for now and I was unable to get a lead into salesforce.com.

So now I am going to try again strictly as you have it.  I am a little perplexed by step 1

For step 1;  I went to the following URL…;



…and then clicked on ‘Fields’.  Is this where I create the checkbox field to “hold whether or not Akismet thinks that it is spam”?  I am thinking it is since steps 3.4 now makes sense.  In which I would just click on 'Web to Lead' from that URL I posted to retrieve the id value.


Thanks again for your time!


Thanks again hemm!  I used your code “as is” and it works smoothly!

I am still unsure why it was not running when I hacked out the Akismet code, but no big deal, I am just glad I can connect to salesforce.com now.  Now I will have an anti-spam feature for the app, even better. :)

ips devips dev
I have tried to send data through curl 
but after submmiting form i am getting error below
please help to shortout 

"Salesforce could not create this lead because of the reason listed below. For more information about this error or help with Web-to-Lead, please contact Customer Support. 
Reason: There's a problem with this country, even though it may appear correct. Please select a country/territory from the list of valid countries. "