Question

Create a droplet with PHP API v2

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?


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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

I have solved it!