7

I have been trying to create a lead from SalesForce's REST API for the past several days but I can't for the life of me get it working. I am able to get the access token no problem but from there on as far as creating a lead I am having absolutely no luck at all.

I keep seeing in all of the documentation this:

curl https://na1.salesforce.com/services/data/v20.0/sobjects/Account/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d @newaccount.json"

How would I do this in PHP's curl though? I have tried and tried but had no luck at all.

Here is how I have got the access token:

$ch = curl_init();

// set URL options

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=".CONSUMER_KEY."&client_secret=".CONSUMER_SECRET."&username=".USERNAME."&password=".USERPASS.SECURITY_TOKEN);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab HTML
$data = curl_exec($ch);

$data = json_decode($data, true);
$code = $data['access_token'];

curl_close($ch);

I have tried doing something like this after this code, however I have had no luck.

$token_url = LOGIN_BASE_URL.'/services/oauth2/token';

$post_fields = array(
    'code' => $code,
    'grant_type' => 'authorization_code',
    'client_id' => CONSUMER_KEY,
    'client_secret' => CONSUMER_SECRET,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

$token_request_body = curl_exec($ch)

I just need to figure out how to create leads in SalesForce, I have no idea where to go from here. Any help would be greatly appreciated as I cannot find decent documentation anywhere that helps me.

Dylan Cross
  • 5,918
  • 22
  • 77
  • 118
  • the code above gets teh token, which you say is working. So we need to see the not working lead creation code and what ever response the api is returning. –  Dec 02 '13 at 19:16
  • That's my question. The second chunk of code is something that I thought was suppose to be how you sent post fields to, however that's not working so it must not be the correct method. I'm stumped at how it's suppose to be done – Dylan Cross Dec 02 '13 at 19:18

1 Answers1

7

create account demo, should get you started:

function create_account($name, $instance_url, $access_token) {
    $url = "$instance_url/services/data/v20.0/sobjects/Account/";

    $content = json_encode(array("Name" => $name));

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
            array("Authorization: OAuth $access_token",
                "Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

    $json_response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ( $status != 201 ) {
        die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
    }

    echo "HTTP status $status creating account<br/><br/>";

    curl_close($curl);

    $response = json_decode($json_response, true);

    $id = $response["id"];

    echo "New record id $id<br/><br/>";

    return $id;
}
  • Thanks so much, this looks like it should work perfectly, but I'm getting an error: Error: call to URL https://na1.salesforce.com/services/data/v20.0/sobjects/Account/ failed with status 401, response [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}], curl_error , curl_errno 0 – Dylan Cross Dec 02 '13 at 19:35
  • your using the url returned with the token ? –  Dec 02 '13 at 19:41
  • Oh well no I wasn't, but now I am. And ah yes as I thought, this creates a new account, not a new Lead. – Dylan Cross Dec 02 '13 at 19:44
  • yes, i provided demo for account, just modify it for lead. i the above example you only need to change the word Account to Lead -ONCE –  Dec 02 '13 at 19:50
  • 4
    all the other fields available to you for a lead are here: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_lead.htm –  Dec 02 '13 at 19:56