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);
Related
I am trying to post a bunch of domains to the godaddy api in order to get information about pricing and availability. However, whenever I try to use curl to execute this request, I am returned nothing. I double checked my key credentials and everything seems to be right on that end. I'm pretty confident the issue is in formatting the postfield, I just don't know how to do that... Thank you to whoever can help in advance!
$header = array(
'Authorization: sso-key ...'
);
$wordsArray = ['hello.com', "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_POST, true); //Can be post, put, delete, etc.
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $wordsArray);
$result = curl_exec($ch);
$dn = json_decode($result, true);
print_r($dn);
There are two problems in your code:
Media type of sent data must be application/json (by default this is application/x-www-form-urlencoded), and your PHP app must accept application/json as well:
$headers = array(
"Authorization: sso-key --your-api-key--",
"Content-Type: application/json",
"Accept: application/json"
);
Post fields must be specified as JSON. To achieve this, use the json_encode function:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));
Full PHP code is:
$headers = array(
"Authorization: sso-key --your-api-key--",
"Content-Type: application/json", // POST as JSON
"Accept: application/json" // Accept response as JSON
);
$wordsArray = ["hello.com", "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));
$result = curl_exec($ch);
$dn = json_decode($result, true);
print_r($dn);
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!
I am using this API
https://market.mashape.com/pareshchouhan/trivia
for making widget in my site but when i am calling this API through CURL it is returning following error:
Any solution for that?
Here my code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Mashape-Key" => "MY_KEY",
"Accept" => "application/json"
));
$data = curl_exec($ch);
curl_close($ch);
echo $data;
when i execute this PHP file, it is returning "Missing Mashape application key" in return data.
http://i.prntscr.com/5f98b1e8965f42eda29875543b052ec5.png
You are specifying you curl headers incorrectly. Look at the CURLOPT_HTTPHEADER option in the docs at http://php.net/manual/en/function.curl-setopt.php. Your code should look like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Mashape-Key: MY_KEY",
"Accept: application/json"
));
$data = curl_exec($ch);
curl_close($ch);
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);
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 );