jdelacrix
By:
jdelacrix

Create a droplet via php using API 2

October 21, 2014 3.9k views

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.

<?php
$url = 'https://api.digitalocean.com/v2/droplets';
$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.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
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;
?>
Thanks.
1 Answer

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:

<?php
$data = array("name" => "TestDroplet", "region" => "ams3", "size" => "512mb", "image" => "ubuntu-14-04-x64");
$data_string = json_encode($data);

$ch = curl_init('https://api.digitalocean.com/v2/droplets');
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);
print_r($result);
?>

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

  • I tested your code and it worked well but i am facing error with passing user_data for installing nginx on droplet after creation .
    How would you add this to your code so that nginx with be installed automatically.

  • @mytechshout07:

    #cloud-config
    packages:
      - nginx
    

    should work. You can pass that as follows:

    <?php
    $user_data = <<<EOD
    #cloud-config
    packages:
      - nginx
    EOD;
    
    $data = array("name" => "TestDroplet", "region" => "ams3", "size" => "512mb", "image" => "ubuntu-14-04-x64", "user_data" => $user_data);
    
    //...
    ?>
    
  • @kamaln7
    Ok got it.
    So if i want to run a bash script instead of just nginx how do it do it ?
    For example a git hub bash script to install lemp stack

  • You can use the runcmd directive to do that:

    #cloud-config
    runcmd:
      - curl https://github-path-to-script | bash
    

    Check out An Introduction to Cloud-Config Scripting | DigitalOcean for more information on how to use Cloud-Config.

    The DigitalOcean metadata service includes a field called "user-data", which can be used to specify a script that will be run as your server is brought online. The CloudInit program, which runs these scripts, can process a special script type called "cloud-config". In this guide, we'll explore how to create cloud-config files and the best ways to leverage their power.
  • +kamaln7:
    How can i do that for shell script. Is that the same ? ?

  • @mytechshout07 You can actually use a bash script in place of the cloud-config file. So just like in @kamaln7 's example above:

    <?php
    $user_data = <<<'EOD'
    #!/bin/bash
    
    apt-get -y update
    apt-get -y install nginx
    EOD;
    
    $data = array("name" => "TestDroplet", "region" => "ams3", "size" => "512mb", "image" => "ubuntu-14-04-x64", "user_data" => $user_data);
    
    //...
    ?>
    

    See An Introduction to Droplet Metadata for more info.

    Update CloudInit link and conclusion Metadata is a service provided to DigitalOcean droplets that allows a droplet to access data about itself, i.e. its metadata. Examples of available droplet metadata include user-provided user data, droplet ID, data center region,...
  • Thanks a lot, its works

Have another answer? Share your knowledge.