user1661677 user1661677 - 5 months ago 56
PHP Question

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

Answer

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;