DigitalOcean
PHP 5.3+ library which helps you to interact with the DigitalOcean API
Install / Use
/learn @toin0u/DigitalOceanREADME
DigitalOcean
The version 2 of the API will be available soon ! Please visit DigitalOceanV2 and contribute :)
This PHP 5.3+ library helps you to interact with the DigitalOcean API via PHP or CLI.
DigitalOcean is built for Developers, helps to get things done faster and to deploy an SSD cloud server in less than 55 seconds with a dedicated IP and root access. Read more.
Installation
This library can be found on Packagist. The recommended way to install this is through composer.
Run these commands to install composer, the library and its dependencies:
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar require toin0u/digitalocean:@stable
Or edit composer.json and add:
{
"require": {
"toin0u/digitalocean": "~1.0"
}
}
And install dependencies:
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install
Now you can add the autoloader, and you will have access to the library:
<?php
require 'vendor/autoload.php';
If you don't use neither Composer nor a ClassLoader in your application, just require the provided autoloader:
<?php
require_once 'src/autoload.php';
Usage
You need an HttpAdapter which is responsible to get data from DigitalOcean's RESTfull API.
You can provide your own adapter by implementing HttpAdapter\HttpAdapterInterface.
<?php
require 'vendor/autoload.php';
use DigitalOcean\DigitalOcean;
use DigitalOcean\Credentials;
// Set up your credentials.
$credentials = new Credentials('YOUR_CLIENT_ID', 'YOUR_API_KEY')
// Use the default adapter, CurlHttpAdapter.
$digitalOcean = new DigitalOcean($credentials);
// Or use BuzzHttpAdapter.
$digitalOcean = new DigitalOcean($credentials, new \HttpAdapter\BuzzHttpAdapter());
// Or
$digitalOcean->setAdapter(new \HttpAdapter\BuzzHttpAdapter());
API
Droplets
// ...
$droplets = $digitalOcean->droplets(); // alias to Droplets class.
try {
// Returns all active droplets that are currently running in your account.
$allActive = $droplets->showAllActive();
printf("%s\n", $allActive->status); // OK
$firstDroplet = $allActive->droplets[0];
printf("%s\n", $firstDroplet->id); // 12345
printf("%s\n", $firstDroplet->name); // foobar
printf("%s\n", $firstDroplet->image_id); // 56789
printf("%s\n", $firstDroplet->size_id); // 66
printf("%s\n", $firstDroplet->region_id); // 2
printf("%s\n", $firstDroplet->backups_active); // true
printf("%s\n", $firstDroplet->ip_address); // 127.0.0.1
printf("%s\n", $firstDroplet->private_ip_address); // null
printf("%s\n", $firstDroplet->locked); // false
printf("%s\n", $firstDroplet->status); // active
printf("%s\n", $firstDroplet->created_at); // 2013-01-01T09:30:00Z
// Returns full information for a specific droplet.
printf("%s\n", $droplets->show(12345)->droplet->name); // foobar
// Creates a new droplet. The argument should be an array with 4 required keys:
// name, sized_id, image_id and region_id. ssh_key_ids key is optional but if any it should be a string.
$createDroplet = $droplets->create(array(
'name' => 'my_new_droplet',
'size_id' => 123,
'image_id' => 456,
'region_id' => 789,
'ssh_key_ids' => '12,34,56', // 3 ssh keys
'private_networking' => true,
'backups_enabled' => true,
));
printf("%s\n", $createDroplet->status); // OK
printf("%s\n", $createDroplet->droplet->event_id); // 78908
// Reboots a droplet.
$rebootDroplet = $droplets->reboot(12345);
printf("%s, %s\n", $rebootDroplet->status, $rebootDroplet->event_id);
// Power cycles a droplet.
$powerCycleDroplet = $droplets->powerCycle(12345);
printf("%s, %s\n", $powerCycleDroplet->status, $powerCycleDroplet->event_id);
// Shutdowns a running droplet.
$shutdownDroplet = $droplets->shutdown(12345);
printf("%s, %s\n", $shutdownDroplet->status, $shutdownDroplet->event_id);
// Powerons a powered off droplet.
$powerOnDroplet = $droplets->powerOn(12345);
printf("%s, %s\n", $powerOnDroplet->status, $powerOnDroplet->event_id);
// Poweroffs a running droplet.
$powerOffDroplet = $droplets->powerOff(12345);
printf("%s, %s\n", $powerOffDroplet->status, $powerOffDroplet->event_id);
// Resets the root password for a droplet.
$resetRootPasswordDroplet = $droplets->resetRootPassword(12345);
printf("%s, %s\n", $resetRootPasswordDroplet->status, $resetRootPasswordDroplet->event_id);
// Resizes a specific droplet to a different size. The argument should be an array with size_id key.
$resizeDroplet = $droplets->resize(12345, array('size_id' => 123));
printf("%s, %s\n", $resizeDroplet->status, $resizeDroplet->event_id);
// Takes a snapshot of the running droplet, which can later be restored or used to create a new droplet
// from the same image. The argument can be an empty array or an array with name key.
$snapshotDroplet = $droplets->snapshot(12345, array('name' => 'my_snapshot'));
printf("%s, %s\n", $snapshotDroplet->status, $snapshotDroplet->event_id);
// Restores a droplet with a previous image or snapshot. The argument should be an array with image_id key.
$restoreDroplet = $droplets->restore(12345, array('image_id' => 123));
printf("%s, %s\n", $restoreDroplet->status, $restoreDroplet->event_id);
// Reinstalls a droplet with a default image. The argument should be an array with image_id key.
$rebuildDroplet = $droplets->rebuild(12345, array('image_id' => 123));
printf("%s, %s\n", $rebuildDroplet->status, $rebuildDroplet->event_id);
// Enables automatic backups which run in the background daily to backup your droplet's data.
$enableBackupsDroplet = $droplets->enableAutomaticBackups(12345);
printf("%s, %s\n", $enableBackupsDroplet->status, $enableBackupsDroplet->event_id);
// Disables automatic backups from running to backup your droplet's data.
$disableBackupsDroplet = $droplets->disableAutomaticBackups(12345);
printf("%s, %s\n", $disableBackupsDroplet->status, $disableBackupsDroplet->event_id);
// Renames a specific droplet to a different name. The argument should be an array with name key.
$renameDroplet = $droplets->rename(12345, array('name' => 'new_name'));
printf("%s, %s\n", $renameDroplet->status, $renameDroplet->event_id);
// Destroys one of your droplets - this is irreversible !
$destroyDroplet = $droplets->destroy(12345);
printf("%s, %s\n", $destroyDroplet->status, $destroyDroplet->event_id);
} catch (Exception $e) {
die($e->getMessage());
}
Regions
// ...
$regions = $digitalOcean->regions(); // alias to Regions class.
try {
// Returns all the available regions within the Digital Ocean cloud.
$allRegions = $regions->getAll();
printf("%s\n", $allRegions->status); // OK
$region1 = $allRegions->regions[0];
printf("%s, %s\n", $region1->id, $region1->name); // 1, New York 1
$region2 = $allRegions->regions[1];
printf("%s, %s\n", $region2->id, $region2->name); // 2, Amsterdam 1
} catch (Exception $e) {
die($e->getMessage());
}
Images
// ...
$images = $digitalOcean->images(); // alias to Images class.
try {
// Returns all the available images that can be accessed by your client ID. You will have access
// to all public images by default, and any snapshots or backups that you have created in your own account.
$allImages = $images->getAll();
printf("%s\n", $allImages->status); // OK
$firstImage = $allImages->images[0];
printf("%s\n", $firstImage->id); // 12345
printf("%s\n", $firstImage->name); // alec snapshot
printf("%s\n", $firstImage->distribution); // Ubuntu
// ...
$otherImage = $allImages->images[36];
printf("%s\n", $otherImage->id); // 32399
printf("%s\n", $otherImage->name); // Fedora 17 x32 Desktop
printf("%s\n", $otherImage->distribution); // Fedora
// Returns all your images.
$myImages = $images->getMyImages();
printf("%s\n", $myImages->status); // OK
$firstImage = $myImages->images[0];
printf("%s\n", $firstImage->id); // 12345
printf("%s\n", $firstImage->name); // my_image 2013-02-01
printf("%s\n", $firstImage->distribution); // Ubuntu
// Returns all global images.
$globalImages = $images->getGlobal();
printf("%s\n", $globalImages->status); // OK
$anImage = $globalImages->images[9];
printf("%s\n", $anImage->id); // 12573
printf("%s\n", $anImage->name); // Debian 6.0 x64
printf("%s\n", $anImage->






