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
yo22yo22 

WEb to Lead from PHP Script

Hi
 
How can I have a Web to Lead generated from  a PHP Script (an existing script we use).
I mean without haveing a form, just directly from the PHP script.
 
Tnx
werewolfwerewolf
Yes, provided you're getting the data from somewhere, you can create leads using the web services API via the PHP Toolkit:

http://wiki.apexdevnet.com/index.php/PHP_Toolkit_11.0
werewolfwerewolf
And that said, you probably don't want to use the actual Web To Lead unauthenticated post functionality.  If you are using a PHP script you may as well do it with the PHP toolkit and the authenticated API (which means that you'll have to hardcode a username and password in your PHP code or config somewhere, which is no big deal because it won't be exposed to the client).  You'll have a lot more flexibility that way.

If you really want to use W2L, someone elsewhere on these boards posted a how-to on doing it with cURL:

http://community.salesforce.com/sforce/board/message?board.id=PerlDevelopment&message.id=3353

But that's a little silly frankly.  Using the API is just simpler and easier.
yo22yo22

Thanks for your reply,

 

I am trying to use this script, which works fine on testing machine (e.g. when sending to a test script URL, I get all the correct POST params in the email).

But I don't get it to work when sending to salesforce. can it be somthing to do with the existing web 2 lead form ?

 

 

Here is my code:

 

include ("somescript.php");

//-------------FUNCTION------------------------------------------------------------------------

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, 'r', 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;
 }

//-----------------USAGE--------------------------------------------------------------------

$url = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8";
//$url = "http://localhost/test_w2l.php";

$data = array (
    'oid' => '00D2000000...,
    'retURL' => 'http://',
    'first_name' => $name,
    'email' => $email,
    'phone' => $phone,
    '00N2000000...' => $the_product,
    '00N2000000......' => $companyMessage
   );
$data = http_build_query($data);

$theresponse = do_post_request($url,$data);

mail($company, $companySubject, $companyMessage."<br>".$theresponse, "From: $email\nBcc: $bcopy\nContent-Type: text/html; charset=iso-8859-1")

 

werewolfwerewolf
There is no existing web to lead form.  You make the web to lead form.  All Salesforce.com really offers is this servlet that you can post a form to.

As I said, doing what you're doing here is a poor substitute for creating the lead via the API.  That would be the path I would take if I were you rather than trying to post to the W2L servlet which was not intended for this purpose.
RadagastRadagast
I have no idea if this will help, as it is an excerpt from a Perl script, not PHP (though I am told they are similar in some ways). I had to learn to modify this script occasionally. It sends form data to SFDC via the web2lead servlet, though it can also send pre-set values instead of user input. I have included below everything that I think is relevant, though I can't explain every line (this was written by a predecessor). Hope it can be of some help, anyway.
 
Code:
#!/usr/bin/perl

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;

my $req = POST 'http://www.salesforce.com/servlet/servlet.WebToLead—encoding=UTF-8',
  [ oid => '000000000000000',   #insert your org ID
 
 # Product Type
 '00N30000001fFKR' => "Volume",   # sending value "Volume" to custom field ID
 
 # Account Tier
 '00N30000001dLnb' => "Commercial",
  
 # Lead Rating
 '00N30000001fFLY' => "A"
    
  ];

$ua->request($req)->as_string;

 

scott4designscott4design
Did this solution end up working out for you?
werewolfwerewolf
Scott,

As I mentioned to you in a private message, he probably could get this to work, but the vastly preferable solution is just to post the data directly to Salesforce.com using the web services API and the PHP Toolkit rather than trying to jerry-rig a post to web-to-lead.
scott4designscott4design
I appreciate your response, but I never received any email from you on this. None the less, I am being asking to implement a means of driving leads directly into our Sales Force. You mention the web services API and the PHP Toolkit but I am having trouble finding this. I am not a PHP developer but I can cannibalize scripts okay. Is there a chance you can point me to this web services API and the PHP Toolkit? Thank you... Scott
Jon Mountjoy_Jon Mountjoy_
Hiya

For PHP toolkit see
http://wiki.apexdevnet.com/index.php/PHP_Toolkit_13.0

For web services API see
http://wiki.apexdevnet.com/index.php/Documentation

Regards,
Jon
Bruno Bonfim AffonsoBruno Bonfim Affonso
Use cURL, it's simple. Don't forget to change the URL to: $url = 'https://test.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
 
//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($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);