如何编写PHP curl DELETE请求

时间:2016-06-26 03:40:54

标签: php curl digital-ocean

我引用了DigitalOcean的API文档,他们在此提供了有关如何删除Droplet的以下示例:

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

如何在PHP curl中编写?

我目前有这个:

{"id":"not_found","message":"The resource you were accessing could not be found."}1

但这不是删除我的Droplet并且正在返回(是的,Droplet id是正确的):

*

2 个答案:

答案 0 :(得分:5)

试试这个

将CUTLOPT_CUSTOMREQUEST更改为“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;

答案 1 :(得分:4)

您需要将CURLOPT_CUSTOMREQUEST设置为DELETE,如PHP手册here中所述。请花些时间阅读。以下代码应该可以使用

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