AddaServerBandwidthUsageMonitorPagewithPHPandVultrAPI

2022-5-16 18:48| 发布者: Hocassian| 查看: 71| 评论: 0|原作者: 樱花庄的白猫

摘要:
C:\Users\Administrator\Downloads\2019-10-14-0-25-44-134316836167100-樱花庄的白猫 ねこ・しろ・ましろ-采集的数据-后羿采集器.html

标题

Add a Server Bandwidth Usage Monitor Page with PHP and Vultr API

标题链接

https://2heng.xin/2017/09/26/php-vultr-bandwidth-en/

post-date

发布于 2017-09-26

post-meta

2,646 热度

评价数

NOTHING

分类

野生技术协会

正文

本文中文版

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.

作者

Mashiro


路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部