如何用数字海洋api v2创建液滴?

时间:2014-12-17 17:32:54

标签: php digital-ocean

我想制作Droplet,但我在curl_close()收到错误。我怎样才能创建Droplet?这给我一个错误:

 Warning: curl_close() expects parameter 1 to be resource 

这是我的代码:

<?php
$data = 'name=TestDroplet&region=ams3&size=512mb&image=449676322';
$create_droplet = curl_init();

curl_setopt_array(
    $create_droplet, 
    array(
        CURLOPT_USERAGENT => "METESTING",
        CURLOPT_POST => 0,
        CURLOPT_URL => 'https://api.digitalocean.com/v2/droplets',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            'Authorization: Bearer 0503e2f686f11fb5b7f81c8baf6434b63103715422cb719711f77dde44d768a4',
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data)
        ),
        CURLOPT_POSTFIELDS => $data
    )
);

$create = curl_exec($create_droplet);
curl_close($create);

echo '<pre>';
print_r($create);
echo '</pre>';

1 个答案:

答案 0 :(得分:3)

您正在尝试curl_close curl_exec的结果,该结果在失败时为FALSE,或为TRUE。在您使用CURLOPT_RETURNTRANSFER的情况下,这是您执行的结果。

$create = curl_exec($create_droplet);
curl_close($create_droplet);