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.
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?
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.
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?
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');
I've been able to use PHP in order to generate JIRA issues, but was wondering if it is possible to also create Confluence wiki pages that link back to the JIRA issue. I haven't been able to make this work yet. Does anyone have any examples on how this is accomplished?
With this code in php, you can create Confluence Pages:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"page\",\"title\":\"inserttitle\",\"space\":{\"key\":\"insertspace\"},\"ancestors\":[{\"type\":\"page\",\"id\":insertancestor}],\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "insertusername" . ":" . "insertpassword");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
You can easily create a new Confluence page using the REST API. Here's an example using curl:
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/
The next thing to do would be for PHP to call that. Check the following for examples:
PHP curl post
Basic authentication in PHP curl
Having done that, you could also embed a JIRA macro in the Confluence page for extra zing. That would mean that the original curl would add the JIRA macro as the storage format (in a Confluence page, click on Tools (or "..." now) and select View Storage Format for an example.
Here's an example of the JIRA macro:
<ac:structured-macro ac:name="jira">
<ac:parameter ac:name="server">Example JIRA</ac:parameter>
<ac:parameter ac:name="serverId">fdsafds-68es-3615-a6f7-71427b983092</ac:parameter>
<ac:parameter ac:name="key">XYZ057-172</ac:parameter>
</ac:structured-macro>
You'll need to figure out your server name, serverId & JIRA issue key yourself.
This would mean the original curl looks like this:
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page with a JIRA macro added:</p><ac:structured-macro ac:name="jira"><ac:parameter ac:name="server">Example JIRA</ac:parameter><ac:parameter ac:name="serverId">fdsafds-68es-3615-a6f7-71427b983092</ac:parameter><ac:parameter ac:name="key">XYZ057-172</ac:parameter></ac:structured-macro>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/