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.
Related
Using the Google Developer Api Tool I have been able to execute a blank query and return a list of contact groups, exactly as expected. When using the same cURL request from my PHP app I don't get anything but some system contact groups (and not all overlap with the tool's response). I can't figure out why this would be the case, am I missing something really obvious?
The Curl Request c&p'd from the google developer tool:
curl \
'https://people.googleapis.com/v1/contactGroups?key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
And my php cURL request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://people.googleapis.com/v1/contactGroups");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = ["Authorization: Bearer {$access_token}", "Accept: application/json"];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
$result = json_decode($data, true);
var_dump($result); die();
The returned outputs of the requests are in the same format, just the content differs. Does anyone have experience trying to get a list of contact groups/labels from the people api?
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.
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');
For now I can send a notification to a single device using the curl command:
curl --header "Authorization: key=AAAxxxxxE4:xxxxxxxuXog" --header Content-Type:"application/json; application/x-www-form-urlencoded;charset=UTF-8" -d '{"to":"DeviceToken","notification":{"title":"Test","body":"Test Message","icon":"icon-192x192.png"}}' https://fcm.googleapis.com/fcm/send
This works great. Now I try to scale the number of recipients by using the topic functionality.
I tried to add a device to the topic "svp" by using the curl command:
curl --header "Authorization: key=AAAxxxxxE4:xxxxxxxuXog" --header Content-Type:"application/json" https://iid.googleapis.com/iid/v1/DeviceToken/topics/svp
Here I get the error message:
{"error":"InvalidToken"}
But I'm sure, both, the Authorization key and the token are correct. (I still can send notifications with it).
Can anybody help me to find the solution for my problem?
I think you missed the rel in your cURL request, between the token and the topic name. As seen in the Instance ID docs, the format should be:
https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME
For some reason, https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME did not work for me. It seemed to me that I was doing everything correctly but what I decided to do was to try the equivalent of that by using https://iid.googleapis.com/iid/v1:batchAdd and sending only one token:
$curlUrl = "https://iid.googleapis.com/iid/v1:batchAdd";
$mypush = array("to"=>"/topics/toronto", "registration_tokens"=>array("acwAw4F8b0W:AMA91bEAKsN1aGjMm5xsobxBHxbYZLfioTPMEIN90njdiK5C2MnOYF4NcOy6ot6BFanMTBIoKRGcyev2RJuydGWt1XHwsniNZ6h8Pjvn9Fqth-Mqgj_5-YN9pt_nMAtgG8blc5bodWyA"));
$myjson = json_encode($mypush);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization:key=[My authorization key]'));
//getting response from server
$response = curl_exec($ch);
I could add the token to the topic successfully. What was returned was this:
{"results":[{}]}
But then I went to confirm to the topics by using https://iid.googleapis.com/iid/info/IID_TOKEN that the token had been successfully added to the topic the way I wanted it and yes, everything worked correctly for me using https://iid.googleapis.com/iid/info/IID_TOKEN. So if you are having problems with https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME, you can simply use https://iid.googleapis.com/iid/v1:batchAdd the way I did it.
Hi I am new to cURL and I don't know how to use it in PHP. I have tried a lot and still in the position where I started. I am trying to do really simple task. Can anyone help me in converting the below cURL command into PHP code....
Please help...
cURL code:
curl https://view-api.box.com/1/documents
-H "Authorization: Token YOUR_API_KEY"
-H "Content-Type: application/json"
-d '{"url": "https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf"}'
-X POST
I need the exact same code to be implemented using PHP. Please help...
Hello I am not totally sure if you should get a response from this call but if you do this should work:
<?php
//Set header information
$headers = array(
"POST HTTP/1.1",
"Content-type: application/json;",
"Authorization: YOUR_API_KEY"
);
//Initialize a curl connection
$ch = curl_init();
//Defines that you get an answer back
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Sets the url to call
curl_setopt($ch, CURLOPT_URL, 'https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf');
//Gives additional headers
curl_setopt($cl, CURLOPT_HTTPHEADER, $headers);
//Executes the curl call
$retrieved_data = curl_exec($ch);
//Closes the connection
curl_close($ch);
//$retrieved_data contains the answer from the curl call
print_r($retrieved_data);
?>
If you don't need a response comment the line with the returntransfer out.
Let me know if it didn't work or it did work :)