I have the follwing curl call which works on linux.
curl -XPOST 'https://site.site.com/1.0/projects/1626/inbox/search.json' -H 'Content-Type: application/json' -H 'Authorization: randomauthorizationcodethatisactuallymuchlongerandmorerandom' --data '{}'
This call works when called in the terminal. I've tried to recreate it using PHP functions and I'm not having any luck. This is what I have done
$accessToken = 'randomauthorizationcodethatisactuallymuchlongerandmorerandom';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://site.site.com/1.0/projects/1626/inbox/search.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, 'Authorization: ' . $accessToken);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
I've been messing with this one for days and I can't figure it out. Any help would be appreciated. Thank you!
Related
I have a cURL command which works very well en CLI (via git Bash). See below:
curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident
This command export very well all issues in an excel file (xls) from my JIRA.
Now i want to transform this command to php curl. I have tried this code below:
$url = 'https://build.bnum.laposte.fr/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident';
$username ='adminid';
$password ='adminpw';
$file = fopen('tvdata.xls', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/vnd.ms-excel"));
curl_setopt($ch, CURLOPT_NOPROGRESS, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15000);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_exec($ch);
curl_close($ch);
fclose($file);
but when i run this php code, he create an empty excel file only, but without to get an error. If somebody can figure out this problem please?
Thanks in advance
Achillix
Have you tried using on-line converters like this one?
From...
curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident
...you get:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "adminid" . ":" . "adminpw");
$headers = array();
$headers[] = "Content-Type: application/vnd.ms-excel";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
...compared with your sample code, CURLOPT_URL and CURLOPT_HTTPHEADER are different, and some other options are not set.
I'm referencing DigitalOcean's API docs, and they give the following example on how to delete a droplet here:
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer API_TOKEN" "https://api.digitalocean.com/v2/droplets/[DROPLET_ID]"
How can I write this in PHP curl?
I currently have this:
$ch = curl_init('https://api.digitalocean.com/v2/droplets/18160706');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer MY_API_TOKEN')
);
$result = curl_exec($ch);
echo $result;
But that is not deleting my droplet and is returning (Yes, the droplet id is correct):
{"id":"not_found","message":"The resource you were accessing could not be found."}1
Try This
Change the CUTLOPT_CUSTOMREQUEST to "DELETE" Find More
$ch = curl_init('https://api.digitalocean.com/v2/droplets/18160706');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer MY_API_TOKEN')
);
$result = curl_exec($ch);
echo $result;
You need to set CURLOPT_CUSTOMREQUEST to DELETE as mentioned in the PHP Manual here. Please take some time to read it. The following code should work
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result);
curl_close($ch);
curl -X POST https://example.com/sandbox -u \
'username:password' -d 'vendor=123456' -d 'list_id=1000001' \
-H 'Accept: application/json
How would I structure a HTTP request with a command cURL like this, with a username/password?
You can use curl in PHP. I've created a example code for you:
$username='username';
$password='password';
$URL='https://example.com/sandbox';
$data=array('vendor'=>123456, 'list_id'=>1000001);
$payload = json_encode( $data );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Accept: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$result=curl_exec($ch);
curl_close ($ch);
I hope that helps :D
Guys. I am new to CURL so i have no experience on implementing CURL request. In this case, i want to post some data using CURL. Here is the CURL :
curl -H 'Authorization: Bearer <ACCESS_TOKEN>' \
-H 'Content-Type: application/json' \
-d '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}' \
https://partner.path.com/1/moment/photo
is there any of you guys know about implementing CURL request using the above data? Thank you very much.
finally, i found the answer. Here is how i create a request to the Path API
$url = 'https://partner.path.com/1/moment/photo';
$authorization = "Authorization: Bearer 8edf232243d58e4940d931490e882123432434f";
$headers = array($authorization,'Content-Type: application/json');
$json_data = '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );
Im trying to make a simple curl request to the desk.com api using PHP but having trouble getting a response. Below is their documentation.
$ curl https://yoursite.desk.com/api/v2/cases/:id \
-u email:password \
-H 'Accept: application/json'
Here is my PHP to try and accomplish the above
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases -u email:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json');
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);
Ive barely used curl before so any help as to why I'm not getting a response would be appreciated.
Have you tried sending basic authorization?
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json');
You also won't need the '-u' in your url, that is meant for command line curl.
Additionally, You'll need to replace ':id' with the ID of the case for the above to return a specific case. For example:
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases/1");
Try this instead, be sure to define/replace $email and $password
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url.desk.com/api/v2/cases");
curl_setopt($ch, CURLOPT_USERPWD, "{$email}:{$password}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json');
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);