HartleySan
HartleySan

Reputation: 7810

How do I make this Salesforce cURL request in PHP?

I'm using PHP to make cURL requests to Salesforce's REST API.
I've got most of the requests I need to make figured out, but I'm not sure how to convert the following curl command on the following Salesforce API page to a cURL request in PHP:

curl https://yourInstance.salesforce.com/services/data/v20.0/sobjects/Account/customExtIdField__c/11999 -H "Authorization: Bearer token" -H "Content-Type: application/json" -d @newrecord.json -X PATCH

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm

I know that that -H option is for headers, which I'm handing with the following:

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

And I think that the -X PATCH part can be accomplished with the following PHP cURL option:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

However, how do I handle the -d @newrecord.json part in PHP cURL?
Thanks.

Upvotes: 1

Views: 2021

Answers (2)

ishegg
ishegg

Reputation: 9937

What you are doing with -d @newrecord.json is uploading a (JSON) file for the endpoint to use. To replicate this in PHP, you need to pass an array with a file element to CUROPT_POSTFIELDS, like this:

$file = [
    "file" => "@newrecord.json";
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);

Make sure to give the correct file path. You can use realpath() to aid with this.

Alternatively, you could just send the JSON encoded data:

$data = [
    "site" => "Stack Overflow",
    "help" => true,
];
$jsonData = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

Don't forget to set your Content-Type: application/json header!

Lastly, your guess about the PATCH request is correct:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

Upvotes: 1

jpalmer4444
jpalmer4444

Reputation: 171

You should POST the json

$post = json_encode($data);

curl_setopt($ch, CURLOPT_POSTFIELDS,$post);

Upvotes: 2

Related Questions