本文中文版

Vultr provides a set of very complete APIs, which allows you to manage your server with almost all the functions that you can do and see in your Vultr's web panel. See Vultr API Docs. So I tried to build a simple server bandwidth usage monitor page with PHP and Vultr's API.

If you have read Vultr's Docs, you'll find that they only told you the call with curl:

curl -H 'API-Key: YOURKEY' https://api.vultr.com/v1/server/bandwidth?SUBID=YOUR_SUBID

Actually, you may do like this, the simplest url:

https://api.vultr.com/v1/server/bandwidth?SUBID=YOUR_SUBID&api_key=YOUR_API_KEY

You will get a feedback in JSON.

It's important that these url or curl calls can only work in authorized IPs, you may add your IPs in this page. Note that "Allow All IP" is not recommended, cause it's not safe that everyone may control your account with your API Key.

Here is the PHP part of souce code(DEMO), and if you want a full monitor page, you may download it at the end of this article:

<?php
error_reporting(E_ERROR); 
ini_set("display_errors","Off");
$request = "https://api.vultr.com/v1/server/bandwidth?SUBID=YOUR_SUBID&api_key=YOUR_API_KEY";
//You need to replace with your server's SUBID and your API key.
$serviceInfo = json_decode(file_get_contents($request));
$income = $serviceInfo->incoming_bytes;
$outgo = $serviceInfo->outgoing_bytes;
$sumin = 0;
$sumout = 0;
foreach ($income as $invalue) {
  $sumin = $sumin + $invalue[1];
}
foreach ($outgo as $outvalue) {
  $sumout = $sumout + $outvalue[1];
}
$data = array(
    "plan_monthly_data" => 1073741824000,
    //Here's your vultr plan, 1073741824000 bits = 1000 Gbits, it's my plan.
    "data_counter" => $sumin + $sumout, 
    );
$json = json_encode($data);
file_put_contents("data.json", $json);
?>

Download Full Souce Code

See Siphils's Bandwagonhost codes.

It's my honor if you like this article, and I'll be very appreciate if you can donate me to support my blog building! (=゚ω゚)ノ

Q.E.D.

Comments