function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ForbiddenForbidden 

Submitting Lead to Salesforce using PHP w/ cURL

Ok,
 
I have seen many posts in regards to submitting a custom form to Salesforce and generating a Lead and the Lead Source Details for Google Adwords.
I have taken the time to write a script that will do the trick for you. Let me know if you have any issues with it. See below...
 
Salesforce Fields
------------------------
oid={OID NUMBER}
retURL={CAN BE ANYTHING, JUST HAS TO BE THERE}
lead_source={LEAD SOURCE NAME}
Campaign_ID={CAMPAIGN_ID}
first_name={FIRST NAME}
last_name={LAST NAME}
company={COMPANY NAME}
landing={LANDING PAGE}
referrer={REFERER PAGE}
 
Example:
------------
oid=****&retURL=http//www.mysite.com/thanks.php&lead_source=Web+Referral&Campaign_ID=****&first_name=First&last_name=Last&company=Company&landing=http://www.mysite.com/myform.php&referrer=http://www.mysite.com/myprevpage.php
 
 
 
 
HTML/Javascript Code:
------------------------------
 
Put there two fields in your form (hidden)
---
<input type="hidden" name="landing" id="landing">
<input type="hidden" name="referrer" id="referrer">
 
 
Put this Salesforce code at the bottom of your page directly above the </body> tag
---
<script type="text/javascript" src="https://lct.salesforce.com/sfga.js"></script>
<script type="text/javascript">__sfga();</script>
<script type="text/javascript">
    var parts    = __kvalueOf('__kts').toString().split(',');
    var landing  = parts[1];
    var referrer = parts[2];
    document.getElementById('landing').value  = landing;
    document.getElementById('referrer').value = referrer;
</script>
 
 
PHP Code w/ cURL
-------------------------
<?php
$salesforce_url = 'http://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$salesforce_data  = 'oid=****&retURL=http//www.mysite.com/thanks.php&lead_source=Web+Referral&Campaign_ID=****';
$salesforce_data .= '&first_name=First&last_name=Last&company=Company';
$salesforce_data .= '&landing=http://www.mysite.com/myform.php&referrer=http://www.mysite.com/myprevpage.php';
 
$salesforce_lead_source_url = 'https://lct.salesforce.com/sfga';
 
if( salesforce_lead($salesforce_url , $salesforce_data) && salesforce_lead_source($salesforce_lead_source_url, $salesforce_data) )
{
    #redirect to thank you page
    header("Location: thanks.php");
    exit;
}
else
{
    print('Error');
    exit;
}
 
Function salesforce_lead($url, $data)
{
    #submit to salesforce as lead
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $auth_result = curl_exec($ch);
    if( curl_errno($ch) ) //error
    {   
        print( curl_error($ch) );
        return false; 
    }
    else // success
    { 
        curl_close($ch);
        return true;
    }
}
 
Function salesforce_lead_source($url, $data)
{
    $t = timeAndMilliseconds();
    $r = $_POST['referrer'];
    $l = $_POST['landing'];
    $oid = '****';
 
    $sfga  = 't='.$t.'&r='.$r.'&l='.$l.'&oid='.$oid.'&ts='.$t.'&rs='.$r.'&ls='.$l;
    $sfga .= '&url='.urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    $sfga .= '&customForm=true&method=post&retURL='.urlencode('http://'.$_SERVER['HTTP_HOST'].'/newsletter-sign-up-thank-you.htm');
 
    #append sfga to the data that was previously submitted to salesforce
    $data .= '&sfga='.$sfga;
 
    #submit to salesforce as lead source to attach to lead
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $auth_result = curl_exec($ch);
    if( curl_errno($ch) ) //error
    {   
        print( curl_error($ch) );
        return false; 
    }
    else // success
    { 
        curl_close($ch);
        return true;
    }
}
 
Function timeAndMilliseconds()
{
    list($usec, $sec) = explode(" ",microtime());
    return (((float)$usec/1000) + (float)$sec);
}
?>


Message Edited by Forbidden on 08-07-2008 07:55 AM

Message Edited by Forbidden on 08-07-2008 09:11 AM

Message Edited by Forbidden on 08-11-2008 05:10 AM
Paul_83ukPaul_83uk
I hold our forms POST data as an array then send it through curl

Could I use your code but add it to the array as below, or would i need to add each of the keys to the array?

Code:
//append sfga data
$cleanPOST['sfga'] = $sfga;

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://lct.salesforce.com/sfga");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
curl_exec($ch); // Post to sfga
curl_close($ch);

 



Message Edited by Paul_83uk on 08-11-2008 08:54 AM
ForbiddenForbidden
Paul,
 
What you have would work fine.
 
You can use your own array as long as you have the fields set in the sfga like my example and the
field being passed with those elements is named 'sfga'.
 
Does this answser your question?
 
Thanks,
Forbidden
Paul_83ukPaul_83uk
Yes thank you, and thank you again for this code it has been really useful.

I have set it up and will now test it to see if it is working correctly.

one further question..

Code:
&url='.urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

 does the url= have to be the URL of the form? As I POST to a seperate page so this would be grabbing the wrong URL. Does this matter?

ForbiddenForbidden

Paul,

Thanks for your comments.
As for that url var you can use the post value of landing if you want since you are posting to a new page.

Thanks,
      Forbidden

Paul_83ukPaul_83uk
Forbidden, Have you got this working in IE7??

From what i can work out, IE7 never seems to set the cookie __kts

it does seem to set a cookie __kti Could I use this cookie for IE?

__kti
1224065138691,http%3A%2F%2Fwww.redwood.com%2Fmisc%2Fadwords%2Fcronacle-job-scheduling%2F%3F_kk%3Dff74ede2-22d2-4a62-84b6-446a893baccf%26_kt%3D2331374401%26gclid%3DCN2J1NDoqJYCFQ0yQgodNFw2yg,
redwood.com/
1088
2350186496
32111674
2465920304
29961901
*

Please let me know if you have got this working in IE7.

Thanks again for your help with all this.
Paul_83ukPaul_83uk
Ok __kti doesn't  seem to work, it comes into SF as "Web Direct"

I'm out of ideas on this one.


abstractjabstractj

IE 7 will actually block out the cookie due to it's predefined browser rule. This is due to the fact lct.salesforce.com does not have a link to it's privacy policy, thus IE7 automatically blocks any cookie from lct.salesforce.com. If you go to Tools > Internet Options > Privacy tab and you should be able to adjust settings for your internet zone.

 

In a nutshell, unless your users has the Accept All Cookies set or accept cookie from lct.salesforce.com site, sfga cookie will be blocked.

jbombjbomb

I'm attempting to submit a web-to-lead form to salesforce using PHP and cURL almost exactly as described above.  I receive the following message when I enable debugging:

 

Your request has been queued. Record Information: oid: XXXXXX amp;debug: 1 amp;retURL: http://example.com amp;first_name: Joe amp;last_name: User .... 

 

However the leads that are generated in SalesForce show the values "[not provided]" for first_name and last_name. 

 

Any suggestions? 

wmuckellwmuckell

I am trying to use your code and although it seems to run ok and I get to the Thank you page, It does not appear in the Saleforce System. I am a little confused over the campaign id too as we seem not to have that. 

 

Thanks in advance

eurorailwayseurorailways

Hey wmuckell, did you manage to get this working?

 

I'm also trying to use the code, but I'm getting the error "transfer closed with outstanding read data remaining" on the "salesforce_lead_source" cURL function call.