Report this

What is the reason for this report?

Create a droplet with PHP API v2

Posted on October 4, 2014

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?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I have solved it!

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.