Last week I was working with salesforce form to collect data from third party website. If you have to just submit form its easy salesforce does not restrict to use CURL in order to post data but my requirement was to post salesforce from data and store that data in my database too. This is easy and simple and has a lot of ways to do.
Now I would like to show you PHP CURL way to post form data. You can use PHP Jquery and Ajax to make it more fancy. But I want to keep it simple.
Step 1 –
I am using one sales force form as example.
Step 2 – This is standard PHP CURL script (form-action-curl.php) to post from you can use anywhere without any modification in from you can add more fields if you need.
//Initialize the $query_string variable for later use
$query_string = "";
//If there are POST variables
if ($_POST) {
//Initialize the $kv array for later use
$kv = array();
//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $key => $value) {
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($key)."=".stripslashes($value);
}
//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
//The original form action URL from Step 2 :)
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
//Open cURL 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($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
//Set some settings that make it all work :)
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
//close cURL connection
curl_close($ch);
if($result=='ok')
{
//echo '<script>alert("Posted -- ")</script>';
}
// Here you can write mysql query to insert data in table.
$insert_tbl_index_page= "insert into tbl_form_data(first_name,last_name,street,city,zip,phone,email)values('$first_name','$last_name','$street','$city','$zip','$phone','$email')";
Hello i have done with above code this is my html code
First Name
Last Name
Address
City
Zip
Email
Phone
This is my PHP code CURL
$value_of_input_field
foreach ($_POST as $key => $value) {
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($key).”=”.stripslashes($value);
}
//Create a query string with join function separted by &
$query_string = join(“&”, $kv);
}
//Check to see if cURL is installed …
if (!function_exists(‘curl_init’)){
die(‘Sorry cURL is not installed!’);
}
//The original form action URL from Step 2 🙂
$url = ‘https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8′;
//Open cURL 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($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
//Set some settings that make it all work 🙂
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
//close cURL connection
curl_close($ch);
if($result==’ok’)
{
//echo ‘<script>alert(“Posted — “)</script>’;
}
// Here you can write mysql query to insert data in table.
$insert_tbl_index_page= “insert into tbl_form_data(first_name,last_name,street,city,zip,phone,email)values(‘$first_name’,’$last_name’,’$street’,’$city’,’$zip’,’$phone’,’$email’)”;
?>
But it is not lead into salesforce
Thanks @tiger
Very GOOD coding OM, specially like the ($_POST) function, that’s wow to me, also the simplicity is epic for this cross-domain posting, but how did I get a $result when
CURLOPT_RETURNTRANSFER, FALSE);
how do i submit a form is the html code for the form is:
there is no name… unlike
any ideas??
This script did exactly what I needed…with one exception. If a formfield is an ARRAY it sends the word “Array.” How can I convert the array into a string to send? Can it be done automatically by looking to see if a field is an array?
THANKS
Thanks for the useful code, but how to make it work with Unicode text (Cyrillic)? Google Drive forms get blank data.
OM, what if the page has multiple forms? Is there a way to add the name attribute of the form?
Thanks for sharing,
Jeff