Create a droplet via php using API 2

  • Posted October 21, 2014

Hi guys, I’m trying to create a new droplet using their API, but when I post the request, it returns blank. Here is my code.

$url = '';
$data = 'name=TestDroplet&region=ams3&size=512mb&image=449676322';
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_HTTPHEADERS => array(
            'Authorization: Bearer Authorization: Bearer MY KEY',
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data)
//execute post
$result = curl_exec($ch);
print_r $result;

Submit an 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.

There are a number of syntax errors that won’t allow your code to run for me. What version of PHP are your running? You may need to set curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); in order to return the results of the request and see what the error from the API call is.

All that aside, here is a PHP snippet that works for me:

$data = array("name" => "TestDroplet", "region" => "ams3", "size" => "512mb", "image" => "ubuntu-14-04-x64");
$data_string = json_encode($data);
$ch = curl_init('');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer YOUR_TOKEN',
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
$result = curl_exec($ch);

You might also want to check out the community contributed PHP bindings for our APIv2.

Hi I am looking for a simple php code that can list all my droplets. I used to use this for v1 API and now it dose not work with v2 API. please help. here is my v1 code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<title>Digital Ocean API Implementation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />



	<table width="700px">
			<td colspan="6">Data Center Control</td>
			<td  style="text-align:center">Server ID</td>
			<td  style="text-align:center">Server Name</td>
			<td  style="text-align:center">IP</td>
			<td  style="text-align:center">Status</td>
			<td  style="text-align:center">Creation Date</td>
			<td  style="text-align:center">Actions</td>

	/* PHP script written by W. Al Maawali  
	# (c) 2014 Founder of Eagle Eye Digital Solutions
	# script starts here:*/
	// Add your own client keys here
	// Add your own API keys here
	// Get current time to speed 
    $loadingtime = time();
	// Get your data from the API provider
	$json = file_get_contents("$myClientID&api_key=$myDOApi");
    $data = json_decode($json);
	// Get live hosts
    $liveCounter = substr_count($json, 'status":"active');
	// Get Offline hosts
	$deadCounter = substr_count($json, 'status":"off');
	// Sum the total
	$counterSum=$liveCounter + $deadCounter; 
    foreach($data -> droplets  as $mydata)
		// Set the droplet id for further actions
			<td  style="text-align:center"><?php echo $mydata->id; ?></td>
			<td  style="text-align:center"><?php echo $mydata->name; ?></td>
			<td  style="text-align:center"><?php echo $mydata->ip_address; ?></td>
			<td  style="text-align:center"><?php echo $mydata->status; ?></td>
			<td  style="text-align:center"><?php echo $mydata->created_at; ?></td>
		    <td class="td_title4" style="text-align:center"><?php echo "<a href=\"$serverid/reboot/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"red\">Reboot</font></a> - <a href=\"$serverid/shutdown/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"red\">Shut Down</font></a> - <a href=\"$serverid/power_on/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"red\">Power On</font></a>" ?></td>
 	}//end for		 
		<td colspan="2">
		Online Droplets: <?php echo  "<font color=\"green\">"  . $liveCounter . "</font>"?><br />
		Offline Droplets: <?php echo  "<font color=\"red\">"  . $deadCounter . "</font>"?><br />
		Total Droplets: <?php echo  "<font color=\"black\">"  . $counterSum . "</font>"?><br />
		<?php echo "Query Time: " . "<font color=\"green\">" . (time() - $loadingtime) . "s </font><br />\n"; ?>


Any devs here care to code an app like this for me? Will pay! Thanks