Skip to content
Advertisement

How to create droplet with digital ocean api v2?

I want to create droplet but I am getting error in curl_close(). How can I create droplet ? It is giving me error like:

 Warning: curl_close() expects parameter 1 to be resource 

This is my code:

<?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>';

Advertisement

Answer

You’re trying to curl_close the result of the curl_exec which is FALSE on failure, or TRUE. In your case since you used CURLOPT_RETURNTRANSFER, it’s the result of your execution.

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

Relevant PHP documentation:

Description

curl_exec ( CurlHandle $handle ) : string|bool

Execute the given cURL session.

This function should be called after initializing a cURL session and all the options for the session are set.

Parameters

handle – A cURL handle returned by curl_init().

Return Values

Returns true on success or false on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, false on failure.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement