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
gyung58gyung58 

Web-To-Lead using PHP Anyone??

I've been searching this for a long time, and I want to be able to send the web-to-lead form using PHP only.

Other people have been trying this, and I've been reading through all the comments, but still haven't been able to figure it out. Currently i'm still trying the fsockopen method. Salesforce said they would get back to me on it.... but they haven't. Anyway this is the script:

function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method)) {
        $method = 'GET';
    }
    $method = strtoupper($method);
    $fp = fsockopen($host, 80);
    if ($method == 'GET') {
        $path .= '?' . $data;
    }
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp,"Content-type: text/html; charset=UTF-8\r\n");
    fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    if ($useragent) {
        fputs($fp, "User-Agent: MSIE\r\n");
    }
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST') {
        fputs($fp, $data);
    }

    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
}

I've tried using it by sending arrays and strings in different formats, such as:

$info = 'oid=\"XXXXXXXXXXX\"' . '&';
$info .= 'retURL=\"XXXXXXXXXXXXXXX\"' . '&';
$info .= 'last_name=\"Yung\"' . '&';


w/o the escapes and also:

   'oid'         => 'oid=XXXXXXXXXXX',
   'retURL'      => 'retURL=XXXXXXXXXXXXXXX',
   'last_name'   => 'Yung',


etc.

I don't want to validate/submit forms with Javascript, and my other idea is having the user confirm the details and submit to salesforce using hidden fields (but spammers can thus clone the page and spam me) while keeping the data in sessions (haven't tested this yet). Anyhelp would REALLY be appreciated, thanks!
SuperfellSuperfell
Off the top of my head, i'd say the content type is wrong, it  proably should be application/x-www-form-urlencoded
gyung58gyung58
Hey thanks for getting back to me. Actually I forgot to mention I tried that too. That seems to kinda work (I get redirected), but like another user, the data doesn't seem to be getting passed into the SF DB. I'll try some more combinations for now, but hopefully someone has gotten it working =)
gyung58gyung58
OK! Mysteriously, it WORKS now! Finally.... weird... I had tried the same combination before, and it didn't.... Anyway, here's the complete code I used. I hope this helps those that haven't been able to figure it out.

Note: I haven't cleaned up the code, so obviously modify it for yourself =)


<?PHP

function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method)) {
        $method = 'GET';
    }
    $method = strtoupper($method);
    $fp = fsockopen($host, 80);
    if ($method == 'GET') {
        $path .= '?' . $data;
    }
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp,"Content-type: application/x-www-form-urlencoded\r\n");
    fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    if ($useragent) {
        fputs($fp, "User-Agent: MSIE\r\n");
    }
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST') {
        fputs($fp, $data);
    }

    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
}

$info = 'oid=0000YOURID' . '&';
$info .= 'retURL=http://URL.COM' . '&';
$info .= 'last_name=TEST FIELD' . '&';
$info .= 'lead_source=SOURCE FROM HERE!' . '&';
$info .= 'company=COMPANY NAME';

$sent = sendToHost('www.salesforce.com','post','/servlet/servlet.WebToLead?encoding=UTF-8',$info);

echo $sent;

?>
diegartediegarte
Hi,
For those willing to use a web2lead form using their own existing online forms (whatever language the form is based on, php, asp, html...) instead of using the default salesforce wen2lead generated code, you should be interrested in trying FormVester for AppExchange.
The application allows to generate new leads in salesforce from any of you existing online forms (simply insert a javascript snipet code in your website pages). FormVester has a deduplicate function, so that it always checks if the lead already exists in salesforce before generating it. If the lead already exists, the lead is simply updated.

Hope it can help. -
<a href="http://www.salesforce.com/appexchange/detail_overview.jsp?NavCode__c=&id=a0330000002YXhxAAG">FormVester for appexchange</a>