Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

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!
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.
Accepted Answer
@PenwellDlamini Give this a shot :)
<?php
$api = 'https://api.digitalocean.com/v2/droplets';
$token = 'your-token';
$options = [
'http' => [
'method' => 'GET',
'header' => "Authorization: Bearer $token"
]
];
$context = stream_context_create( $options );
$results = file_get_contents( $api, false, $context );
$results = json_decode( $results );
?>
<table class="table">
<thead>
<tr>
<th style="text-align:center">Droplet ID</th>
<th style="text-align:center">Server Name</th>
<th style="text-align:center">Status</th>
<th style="text-align:center">Public IPv4</th>
<th style="text-align:center">Private IPv4</th>
<th style="text-align:center">Public IPv6</th>
</tr>
</thead>
<?php
foreach ( $results as $droplets )
{
foreach ( $droplets as $droplet )
{
if ( isset( $droplet->id ) )
{
?>
<tr>
<td style="text-align:center"><?php echo $droplet->id; ?></td>
<td style="text-align:center"><?php echo $droplet->name; ?></td>
<td style="text-align:center"><?php echo $droplet->status; ?></td>
<td style="text-align:center"><?php echo $droplet->networks->v4[1]->ip_address; ?></td>
<td style="text-align:center"><?php echo isset( $droplet->networks->v4[0]->ip_address ) ? $droplet->networks->v4[0]->ip_address : 'No Private IP'; ?></td>
<td style="text-align:center"><?php echo $droplet->networks->v6[0]->ip_address; ?></td>
</tr>
<?php
}
}
}
?>
</thead>
</table>
That being said, i would definitely recommend using a library, such as :
https://github.com/toin0u/DigitalOceanV2
… or a similar library. It’ll simplify this quite a bit since the library will handle the API, Token, and the method calls.
Please make sure you replace my-token for the $token variable.
What output are you seeing when trying to run the script? Are any errors presented?
Another option for you would be to use a pre-built library for the DO API rather than handling the json directly. This project provides a PHP API client you can use in your scripts.
thank you, this works. However, how to display droplets that exist in a particular project