How do I make this Salesforce cURL request in PHP? - 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.

You should POST the json
$post = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);

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');

Related

PHP CURL issue with an API when retrieving datas

we have an API existing with a distributor.
Our software is running under PHP 7.3.6 / Apache 2.4.38
We already successfully did some other actions: creating new purchase orders, retrieving orders, ...
We have a problem to retrieve invoices.
We are using for our API tests a software called POSTMAN.
We input all the informations (api key, ....)
Using POSTMAN, it works perfectly. There is an option in POSTMAN to obtain the code in different langage. For our needs, we took the PHP generated code. The problem is that it is not working.
We also used https://reqbin.com/curl and it works perfectly. But same problem, the generated code in PHP is not working.
For example, in postman or reqbin, this CURL code is working
curl -X GET https://url.com/Invoices?startDate="01-01-2019"&endDate="01-31-2023" \
-H 'Accept: application/json' \
-H 'Authorization: Bearer generatedtokenzzzzzzzzzzzzzzzzzzzzzzzzzzz' \
-H 'Content-Type: application/json' \
-H 'zzzzzzzzz-API-Key: generatedapikeyzzzzzzzzzzzzzz'
when we click on generate PHP code we have this code:
<?php
$url = "https://url.com/Invoices?startDate="01-01-2019"&endDate="01-31-2023";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Bearer generatedtokenzzzzzzzzzzzzzzzzzzzzzzzzzzz",
"Content-Type: application/json",
"zzzzzzzzz-API-Key: generatedapikeyzzzzzzzzzzzzzz",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
Because it didn't work , we tried to add some options like
CURLOPT_CUSTOMREQUEST => 'GET',
and it still don't work.
We are getting crazy, we are on this problem for more than a week....
Any help would be very usefull.
Many thanks by advance....
We find the bug.
it was not in the part of this code but it was above.
a parameter was not passed correctly, and the error message {message": "list index out of range} was in fact wrong.

No response on paypal api with php curl

I'm trying to get the response from the PayPal api regarding an order with php and cURL. Here is my code, I have of course replaced the token, which is not the problem here.
With the following command, everything works fine in my terminal, I don't understand why it doesn't work. (If I do a var_dump($response), I get string(0) "".
Code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api-m.sandbox.paypal.com/v2/checkout/orders/".$_GET['transacid']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"Content-Type: application/json",
"Authorization: Bearer my_oauth_token"
];
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response= curl_exec($ch);
curl_close($ch);
var_dump($response);
And, the CLI command :
curl -v -X GET https://api-m.sandbox.paypal.com/v2/checkout/orders/my_order_id\
-H "Content-Type: application/json" \
-H "Authorization: Bearer my_oauth_token"
I think I can get the result with an exec(), but it's really not clean, especially with a GET variable.
Thanks !
(Posting as answer after commenting)
Just a remark that might not be related but in the terminal, you are doing a GET request while on the php side you are doing a POST request (cf. CURLOPT_POST which is set to 1). So you are not doing the same type of request already. First do that and then we can troubleshoot further if needed. And eventually, that might be the only thing causing your issue.

uploading a video file with curl request in php

Hi i'm facing curl requests for my first time and i'm having problem with a particular one.
I want to upload a video to spreaker site by php request. I have no problem with authentication or text areas, but everything i try i can't upload the media_file. Here's the code:
$authorization = "O_AUTH_CODE";
$url = 'https://api.spreaker.com/v2/shows/n_show/episodes';
$test=curl_init($url);
curl_setopt($test, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data' , $authorization));
curl_setopt($test, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($test, CURLOPT_POSTFIELDS, array(
'title'=>'MyTitle',
'media_file'=>'#file_path',
));
curl_exec($test);
curl_close($test);
and the result is this:
{"response":{"error":{"messages":["media_file: Required."],"code":400}}}
The request that the api documentation ask is this:
curl -X POST -H "Authorization: Bearer OAUTH-TOKEN" -F media_file=#audio.mp3 -F "title=Daily News" https://api.spreaker.com/v2/shows/1/episodes
The only thing i can notice is that the media file is not passed by string, but i can't figure out how i can do. Some help?

Cannot reproduce simple POSTMAN query into php curl

I can send a POST request successfully with POSTMAN and when I turn the request to code I get:
curl --location --request POST 'https://webservice.apiCommerce/import' \
--form 'id=1186' \
--form 'image=#/C:/collection/img/ecomm/01.jpg'
Now, I am trying the same thing with PHP curl and I don't get the expected result.
$imagePath = "./img/$fileName";
$post = [
"id" => $id,
"image" => '#'.$imagePath
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$payload = json_decode($response, true);
The request gets sent, but the image doesn't get uploaded correctly. Is there a reason why there's a mismatch? I can't figure out what's wrong. I tried removing the header and so forth, but it doesn't seem to make any difference at all. I even hard-coded the img path and it doesn't seem to work.
The first request is succeeding because it is being encoded as multipart/form-data, as uploads should be. As the curl --help menu explains, the --form name=value command specifies multipart MIME data, as opposed to the --data option and its variants. In the second request, however, you are specifying the content type as application/x-www-urlencoding. This encoding is usually preferred, but will not in this case do what you want.
From the MDN web docs:
Non-alphanumeric characters in both keys and values are percent encoded: this is the reason why this type is not suitable to use with binary data (use multipart/form-data instead)

replicating command line cURL in PHP

I'm integrating with a 3rd party's API, I have to POST some XML and I get some XML back.
On the CLI this works, I get a positive response.
curl -X POST -d #/tmp/file http://url/to/endpoint --header "Content-Type:application/x-www-form-urlencoded"
This, however, does not work, the response contains an error telling me my that my request XML is invalid.
$ch = curl_init();
$post = array(
'file' => '#/tmp/file'
);
curl_setopt($ch, CURLOPT_URL, 'http://url/to/endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$this->responseBody = curl_exec($ch);
curl_close($ch);
It's the same file in both cases and it's on the same server. the file is just plain text XML. The only difference that I can see is that I'm specifying a fieldname in my HTTP headers on the PHP version.
How do I send that file over using PHP to exactly replicate the CLI version, e.g. without the formdata/fieldname bit?
FWIW I can't go back to the developer of the API for a few days to ask what he's defining as 'bad XML'
Try passing the file as raw data, not in an array, by for example using file_get_contents().
So instead of:
$post = array('file' => '#/tmp/file');
Like this:
$post = file_get_contents('#/tmp/file');

Categories