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
dashboreddashbored 

Cannot generate web-to-lead using curl or fopen


Hello,

I'm working with PHP in Wordpress, and I'm trying to generate a web-to-lead using PHP code, but I'm having no luck.  I am able to generate a lead entering the following in my web browser:
https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8&lead_source=General&oid=myorgid&idClient=99&first_name=DonDon&last_name=Trump&email=Don%40famousknobs.com&phone=212-853-7999&description=Dufusry

and I am able to generate a lead using an HTML form:
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

    <input type=hidden name="lead_source" value="Tier1">
    <input type=hidden name="oid" value="myorgid">

    <input id="first_name" name="first_name" type="text" value="" style="width:95%" />
    <input id="last_name" name="last_name" type="text" value="" style="width:95%" />
    <input id="email" name="email" type="text" value="" style="width:95%" />
    <input id="phone" name="phone" type="text" value="" style="width:95%" />
    <input id="description" name="description" type="text" value="" style="width:95%" />

    <input id="submit"type="submit" value="Do it"></input>

</form>


But when I use curl or fopen or http_post_fields (code below), I get nothing, and no indication of what could be wrong.  Can anyone tell me how to do this, or give me some tips on what to look for?

By the way, submitting directly from the form is not an option because I have to do some processing locally prior to actually submitting the lead.  So right now the HTML form above is actually set up to load a page where that processing occurs and where the lead is sent out.

Thanks!
Dave

function do_post_request($url, $data, $optional_headers = null) {
    $params = array('http' => array(
                'method' => 'POST',
                'content' => $data
                ));
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
    }
    return $response;
}

$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

//url-ify the data for the POST
foreach ($_POST as $key=>$value) {
    $postvalues[$key] = $value;
    $postFieldsString .= $key.'='.urlencode($value).'&';
}
rtrim($postFieldsString,'&');

//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($postvalues));
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvalues);

//execute post using curl
$resultOfPost = curl_exec($ch);

//close connection
curl_close($ch);

//execute post using fopen
$dingoAteMyBaby = do_post_request($url, $postvalues);

//execute post using http_post_fields
$lordHaveMercy = http_post_fields("https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8&", $postvalues);

sjain1.2523113033806995E12sjain1.2523113033806995E12

Hi,

       Are you doing web to lead in developer edition or sandbox edition.Make sure your above code works for developer Org.If you are working on sandbox org then you have to use test.salesforce.com rather than www.salesforce.com