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 :)
Related
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 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.
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 have a curl request to get some information about database and stuff, but since some user need to query many bases and most of them are not familiar with any kind of shell,
we are trying to build a small page to e"xecute the query and be more user friendly
we got pretty much everything working (form and stuff) but we got really figure how to build the uery in php
here is the original working curl request we made:
curl -s - X POST -H 'Content-type:application/json' -H
'Accept:application/json' -d '{"param1":"value1","param2":"value2"} -k
https:myurl -u user:password
and here is the code with the request we are trying to build:
<?php
$data = array('param1' => 'value1',
'param2' => 'value2'
);
$ch = curl_init();
$url = "https://myurl";
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch,curlopt_userpwd, "user:password" );
curl_setopt($ch,curlopt_post, 1);
curl_setopt($ch,curlopt_postfuelds, $data);
curl_setopt($ch,curlopt_returntransfer, true);
curl_setopt($ch,curlopt_ssl_verifypeer, false);
curl_setopt($ch,curlopt_httpheader,
array ("Content-type:application/json"));
$output = curl_exec($ch);
curl_close($ch);
I cannot really figure whats wrong? can you help me?
I rather not use shell_exec with the curl inside because the server hosting is on windows, and we don't have curl.exe available
thanks
You have a typo: curlopt_postfuelds should be curlopt_postfields
I have spent countless hours trying to get the Attachment Resource API(s) to work with no avail. I have referred to the docs here: http://docs.getzephyr.apiary.io/#executionresourceapis
But they are not much help and Zephyr support has not responded to any of my questions in the last 3 months.
Here is my curl call:
curl -D- -u user:pass -X POST -H "Content-Type: multipart/form-data" -H "X- Atlassian-Token: nocheck" -F "file=/home/jared/apiautomation/output.html" "https://jiraurl/rest/zapi/latest/attachment?entityId=3019&entityType=execution"
I have also tried php:
<?php
$url = "http://jiraurl/rest/zapi/latest/attachment?entityId=3091&entityType=execution";
$upass="";
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, $upass);
$file_name_with_full_path = realpath("/home/jared/postman/authentication/output.html");
$post = array("file=#.$file_name_with_full_path; filename=output.html;");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Atlassian-Token: nocheck'));
$response = curl_exec($curl);
curl_close ($curl);
?>
for both examples i get unsupported media type. which doesnt make sense because I can attach it through Jira. Im completely lost at this point. Ive referenced:
https://answers.atlassian.com/questions/268253/add-attachment-to-test-execution-using-zapi
Please help. :)
This was answered on Atlassian Answers but here is a copy for this question here.
1) entityType
This is the item you will be attaching to. Currently the only types are 'Execution' and 'TestStepResult'
2) entityId
This is the actual Id of the item whose type you chose. If it is an Execution type, you will need a 'scheduleId'. If it is a TestStepResult, you will need a 'testStepId'
The CURL for adding an attachment via ZAPI is formatted as:
curl -D- -u : -X POST -H "X-Atlassian-Token: nocheck" -F "file=#name_map.jpg" "http://192.168.100.144:9122/rest/zapi/latest/attachment?entityId=&entityType="
Note: "X-Atlassian-Token: nocheck" is required
For sample code in Python of attaching to a entity of type 'Execution', please see our Community Forum post here:
http://community.yourzephyr.com/viewtopic.php?f=21&t=1382
Regards