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?
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');
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
Or does PHP not allow this? I have read it is possible using PUT but the server is expecting POST only.
cURL has already support for streams, try curl --help | grep binary and you will get:
--data-binary DATA HTTP POST binary data (H)
An example:
curl -v -XPOST http://example:port/path --data-binary #file.tar
-H "Content-Type: application/octet-stream"
Yes it is possible to stream upload a file using POST file uploads with cURL. This is the default and you need to provide the filename of the file(s) you would like to stream in form of strings.
// URL on which we have to post data
$url = "http://localhost/tutorials/post_action.php";
// Any other field you might want to catch
$post_data['name'] = "khan";
// File you want to upload/post
$post_data['file'] = "#c:/logs.log";
// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
// Just for debug: to see response
echo $response;
Apart from that default method, it is not possible to use cURL to stream upload a file using the POST method.
Command to upload a binary file
curl --location --request POST 'http://localhost:8080/hello/read' \
-H "Content-Type: application/octet-stream" \
--data-binary '#/Users/krishna/Documents/demo.bin'
command to upload a text file
curl --location --request POST 'http://localhost:8080/hello/read' \
--header 'Content-Type: text/plain' \
--data-binary '#/Users/krishna/Documents/demo.txt'
Set the Content-Type as per the file content.