Make use of Digital Ocean and Vultr VPS API with PHP wrapper

Make use of Digital Ocean and Vultr VPS API with PHP wrapper

Here is my first experience with api wrapper. Digital Ocean and Vultr provides vps services so if you don’t want to log into their panel each time you want to perform an action to the vps node you can call the droplets functions from your own website and have your own control panel. First you will need to create your own client ID and key both can be generated from Digital Ocean user control panel.

Here is the code that I wrote to list all the droplets and perform actions like reboot, shutdown, and power on:

 

DO first method:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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" />
</head>

<body>

           
	&nbsp;&nbsp;&nbsp;&nbsp;

	<table width="700px">
		<tr>
			<td colspan="6">Data Center Control</td>
		</tr>
		<tr>
			<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>
		 
		</tr>

	<?php
	/* PHP script written by W. Al Maawali  
	# (c) 2014 Founder of Eagle Eye Digital Solutions
	# http://www.digi77.com
	# http://www.om77.net
	# script starts here:*/
	
	
	 
	// Add your own client keys here
	$myClientID="xxxxxxxxxxxx"; 
	// Add your own API keys here
	$myDOApi="xxxxxxxxxxxxxxxxxxxx"; 
	
	
	// Get current time to speed 
    $loadingtime = time();
    
	// Get your data from the API provider
	$json = file_get_contents("https://api.digitalocean.com/v1/droplets/?client_id=$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
		$serverid=$mydata->id;
	?>
	        
			<tr>				    
			<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=\"https://api.digitalocean.com/droplets/$serverid/reboot/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"red\">Reboot</font></a> - <a href=\"https://api.digitalocean.com/droplets/$serverid/shutdown/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"red\">Shut Down</font></a> - <a href=\"https://api.digitalocean.com/droplets/$serverid/power_on/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"red\">Power On</font></a>" ?></td>
		   
	<?php
 	}//end for		 
	?>
</tr>
<tr>
		<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"; ?>
		</td>
			
			
		</tr>
	</table>	 

</body>
</html>


 

Download


 

DO second method:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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" />
</head>

<body>



           
	&nbsp;&nbsp;&nbsp;&nbsp;

	<table width="700px">
		<tr>
			<td colspan="6">Data Center Control</td>
		</tr>
		<tr>
			<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>
		 
		</tr>

	 <?php
	  /* PHP script written by W. Al Maawali  
	  # (c) 2014 Founder of Eagle Eye Digital Solutions
	  # http://www.digi77.com
	  # http://www.om77.net
	  # script starts here:*/
	
	 //<!-- Api stuff starts here -->
	//talk to digital ocean via API
	require_once('DO.class.php');
	$myClientID="Your_Own_Client_ID_From_Digitalocean"; // Add your own client keys here
	$myDOApi="Your_Own_API_From_Digitalocean"; // Add your own API keys here
	$ocean = new \DigitalOceanApi\DigitalOcean($myClientID,$myDOApi);    
	$droplets = get_object_vars($ocean->getDroplets());
	$m=0;

	foreach ($droplets as $drops) 
	{
		if (is_array($drops)) 
		{
			
			//get number of droplets
			$DropletArrayValues= serialize($droplets);
			$NumberOfDroplets=substr_count($DropletArrayValues, 'image_id');
			 
			while($m<$NumberOfDroplets)
			{
				//get id of each droplet					
				$serverid=$drops[$m]->id;

	?>
	 
			<tr>				    
			<td  style="text-align:center"><?php echo $drops[$m]->id; ?></td>
			<td  style="text-align:center"><?php echo $drops[$m]->name; ?></td>
			<td  style="text-align:center"><?php echo $drops[$m]->ip_address; ?></td>
			<td  style="text-align:center"><?php echo $drops[$m]->status; ?></td>
			<td  style="text-align:center"><?php echo $drops[$m]->created_at; ?></td>
		  <td class="td_title4" style="text-align:center"><?php echo "<a href=\"https://api.digitalocean.com/droplets/$serverid/reboot/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"#CCFB5D\">Reboot</font></a> - <a href=\"https://api.digitalocean.com/droplets/$serverid/shutdown/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"#CCFB5D\">Shut Down</font></a> - <a href=\"https://api.digitalocean.com/droplets/$serverid/power_on/?client_id=$myClientID&api_key=$myDOApi\" target=\"_blank\"><font color=\"#CCFB5D\">Power On</font></a>" ?></td>
		   
	<?php


	$m++;
			}//end while
		}//end if

	}//end for
		
		
		//print all information for debug only
		//print_r(array_values($droplets));		
	?>
</tr>
<tr>
		<td colspan="2">
				Total Droplets: <?php echo  "<font color=\"red\">"  . $NumberOfDroplets . "</font>"?><br />
		</td>
			
			
		</tr>
	</table>		
	
	
	<!-- Api stuff ends here -->

</body>
</html>

You will need DO.class.php to be on the same folder as this script:

Download DO.class.php class from here.

Download do.php code from here.

Sample of a working code on Stealthwalker admin control panel:

DigitalOcean-do-api

DigitalOcean-do-api

You can perform more actions as long you have the node id available actions can be found here.
 


 

 

Vultr method:

 

My new experiance is with Vultr Cloud VPS provider you can see their API here. Now let me show you how to wrap it with PHP code its simple with the same concept above except you won’t have an array.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Vultr API Implementation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
</head>
 
<body>
 
           
	&nbsp;&nbsp;&nbsp;&nbsp;
 
	<table width="700px">
		<tr>
			<td colspan="6">Data Center Control</td>
		</tr>
		<tr>
			<td  style="text-align:center">Server ID</td>
			<td  style="text-align:center">Server Name</td>
			<td  style="text-align:center">City</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">Bandwidth</td>
			<td  style="text-align:center">Backups</td>
			 
		 
		</tr>
 
	<?php
	/* PHP script written by W. Al Maawali  
	# (c) 2015 Founder of Eagle Eye Digital Solutions
	# http://www.digi77.com
	# http://www.om77.net
	# script starts here:*/
	
	
		
	// Add your own API keys here
	$myVultrApiKey="Get your own key from vultr.com"; 
	
	
	// Get current time to speed 
    $loadingtime = time();
    
	// Get your data from the API provider
	$json = file_get_contents("https://api.vultr.com/v1/server/list?api_key=$myVultrApiKey");
    $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 as $name => $value)  
    {
	
 
	?>
	        
			<tr>				    
			<td  style="text-align:center"><?php echo $value->SUBID; ?></td>
			<td  style="text-align:center"><?php echo $value->label; ?></td>
			<td  style="text-align:center"><?php echo $value->location; ?></td>
			<td  style="text-align:center"><?php echo $value->main_ip; ?></td>
			<td  style="text-align:center"><?php echo $value->power_status; ?></td>
			<td  style="text-align:center"><?php echo $value->date_created; ?></td>
			<td  style="text-align:center"><?php echo $value->current_bandwidth_gb; ?></td>
			<td  style="text-align:center"><?php echo $value->auto_backups; ?></td>

	<?php
 	}//end for		 
	?>
</tr>
<tr>
		<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"; ?>
		</td>
			
			
		</tr>
	</table>	 
 
</body>
</html>

 
Download Vultr Api wrapper code from here.
 
cp-ips

 


 

Digiprove sealCopyright protected by Digiprove © 2014-2016 Eagle Eye Digital Solutions
JOIN OUR NEWSLETTER
Amazing people have subscribed to our newsletter — and you’re amazing too!
We hate spam. Your email address will not be sold or shared with anyone else.
The following two tabs change content below.
Warith Al Maawali
W. AL Maawali is the Founder and Chief Editor of Eagle Eye Digital Solutions from the Sultanate of Oman with over 20 years experience in Security and Digital Forensics. He is also the Founder of om77.net.
PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.
In computer programming, an application programming interface (API) is a set of routines, protocols, and tools for building software applications. An API expresses a software component in terms of its operations, inputs, outputs, and underlying types.

15 comments

  1. I am impressed with the ideas you have we can also provide both centralized or take away manual installation script. I like your first approach where have an independent product and let it evolve as the market matures I will put some time into this and study the market stay tuned if I decide to give it a go I will need your valuable experience.

    Thank you Terence

    • Terence

      You are welcome and I am happy to be a beta tester. You can always get me at terence dot milbourn at gmail dot com.

    • Terence

      By the way, Tugboat is the only app I found so far that comes anywhere near it, and its a whole interface away, but I guess you already know that.

      Terence.

      • Hi Terence,

        I agree Tugboat does the job as required but in command line mode no GUI for it yet plus you need Linux to run it. If you notice on the post I have rewritten the code again under method one its more simple now no need for complicated classes.

        Thank you for sharing 🙂

        • Great tutorial! I was looking for something like this recently but more specifically for WHMCS. I found a module already written but it costs per month so this will help me create my own module! 🙂

  2. Sure I will look into them Terence, I appreciate your valuable feedback :).

  3. Terence

    Hi,

    Have you given any thought to developing this as a product? Perhaps adding support for the Linode API too? I am not a developer so I can’t help you there, but I am a long-time software marketer and I’d love to sell it.

    Terence.

    • Hi Terence,

      I haven’t thought of it but its a good idea. I only wrote it because I am a customer of DO and I wanted to make node management easier.

      You will be the first to contact whenever I plan to develope one thanks for your valuable input.

      • Terence

        Hi Warith,

        I have done quite a bit of research over the last few months and there is quite definitely a hole in the market for a simple control panel to manage multiple VPS nodes.

        Not only that, everything out there already ~ and I have personally tested 18 control panels ~ either is bloated with unnecessary open-source utilities, or just doesn’t work properly, or BOTH!

        Its a nightmare when the OS installs faultlessly in a couple of minutes, but the control panel grinds on and installing for 15 minutes or so, and then falls over due to missing dependencies.

        The way to do it is ~

        Make it secure.

        Keep it simple.

        Pray its fast… 😉

        Terence.

        • Hi Terence,

          This sounds interesting from your research it seems that it would definitely get consumers attention if its well implemented.

          If you don’t mind me asking what would be your preference as a user ? Is it to have an install script on your own site to control your own nodes for a specific vendor or to have a central solution with no setup hassle where you only login into it enter the API details once of the service you would like to take control. Therefore you can have all the vendors in one secured centralized control panel and the API management product provider takes care of any API changes or updates.

          Thank you 🙂

          • Terence

            Hi Warith,

            With the rise of cheap SSD VPS’s, I think there’s a growing market for this kind of product.

            Most of the ‘free’ and proprietary panels are still focused on shared hosting and not VPS’s on multiple servers.

            To make it work though, you need to pull together quite a few different pieces of software from other players; not all in the Open-Source market. Which, with up-front, one-time and monthly license fees, can make a total solution a tad expensive.

            For example, ServerPilot will install your a PHP 5.4/5 OpCache Nginx server in-front of Apache and make it all blindingly fast ~ for free ~ then you can install your apps. Or you can use Easy Engine and have an optimised multisite WordPress installation on its own Nginx server in less than a minute ~ again free.

            But, when you come to selling and managing these servers and having customers pay you, the cPanel, WHMCS and Blesta teams seem to have the market by the shorts.

            Pulling that kind of fire-power together is always going to be a process, not a product. You may well end up with an all singing and dancing application, but you need to start out simple and the product will take time to evolve as the market matures.

            From my own perspective, I would prefer to run the script on my own server/s. But I know many others would prefer ~ or are technically challenged and have no choice ~ to let others have the hassle of installation, set-up and maintenance.

            If I was selling it I would give away the former ~ I am not talking a crippled ‘freemium’ product here ~ and charge for the latter. Just like WordPress do.

            What do you think about that?

            Terence.

          • Terence

            Warith,

            Another thought… why not simply build a plugin for an existing panel like VestaCP, or one of the others, and give yourself immediate access to their market?

            Terence.

          • Terence

            Warith,

            If you want some help with this proect, check out this thread. 😉

            Terence.

        • Hi there, thanks for your comments. I am having trouble deciding which is best to run a VPS on Digital Ocean: easyengine or Serverpilot? What do you think?

          • Hello Joe,

            If your are planning to install a PHP 5.4/5 OpCache Nginx server in-front of Apache as Ternce mentioned earlier go for server pilot. You can also check Softaculous and Ammps from here

            Here is a good article to help you decide..

            If I was you I would try the top three then decide which one suites my needs.

commentJoin the Discussion

Pin It on Pinterest