damir
By:
damir

Create a droplet with PHP API v2

October 4, 2014 2.5k views

Hi,

I am trying to create an droplet with php and I have read the https://developers.digitalocean.com/v2/#create-a-new-droplet on how it could be achieved but I haven't really got it working yet.

My PHP code is as following:

$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_HTTPHEADERS => array(
            'Authorization: Bearer ACCESS_TOKEN',
            '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>';

I get this error --> {"id":"unauthorized","message":"Unable to authenticate you."}.

I have checked the ACCESS_TOKEN and it is correct. I have even regenerated it several times and I have it on read, write support.

Does anyone know what I might be missing?

2 Answers

CURLOPT_HTTPHEADERS should be CURLOPT_HTTPHEADER, see PHP: curl_setopt - Manual for the list of available options.

  • Thanks kamaln7. That helped.

    I get an error saying You specified an invalid image for Droplet creation..
    I tried the image number in the document but same error. I changed it to some random number and still same error.

    What does it mean?

I have solved it!

Have another answer? Share your knowledge.