Create a cURL request using PATH API - php

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 );

Related

How to get survey ID by name in SurveyMonkey

How to get survey ID by name in SurveyMonkey?
This is what is found, but how to convert this into PHP?
curl -i -X POST -H "Authorization:bearer YOUR_ACCESS_TOKEN" -H "Content-Type": "application/json" https://api.surveymonkey.net/v3/surveys -d '{"title":"New Survey"}'
Here is my basic api call without passing the survey title parameter:
<?php
$requestHeaders = array(
'Content-Type: application/json',
'Authorization: Bearer 12345',
);
$url = 'https://api.surveymonkey.net/v3/surveys/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_contactlist);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
?>
Thanks.
Solution:
Add
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
i found the solution: add the following lines:
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

How to write PHP curl DELETE request

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);

Getting cURL call to work in PHP -XPOST

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!

Convert cURL command to cURL PHP

curl -X GET --header "Accept: application/json" --header "authorization: <API token>" "https://api.clashofclans.com/v1/locations"
^ That's the cURL command I want to convert. I tried looking for tutorials on cURL but couldn't understand it. Current code I tried is as follows:
$ch = curl_init('https://api.clashofclans.com/v1/clans/%2399VY9JR8');
curl_setopt($ch,CURLOPT_HTTPHEADER,array('authorization: myToken','Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
Trying to achieve getting response from API and posting then reading them here.
In get request you just have to pass the params with the URL itself, following code should work:
$ch = curl_init();
$url = "https://api.clashofclans.com";
$clansId= "2399VY9JR8";
$headers= array("authorization: myToken");
curl_setopt($ch, CURLOPT_URL, $url.'clans/'.$clansId);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);

translating a command line curl into php

i am not very curl savvy was wondering if anyone could help me turn the following into php:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}' https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret
if you know of a good curl tutorial would also be great help.
something like this
$ch = curl_init();
$json = '{"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}';
$url = 'https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret';
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

Categories