#教程# – 基于solusVM面板API调用的VPS流量统计页面(PHP版本)

前言

我们购买的 VPS 大部分都是有流量限制的,怎么简单又方便的查看当前使用的流量呢?以往我们不是登录 VPS、solusVM面板管理后台,就是安装探针查看。但觉得登录后台太麻烦了,探针呢只是统计网卡的流量,不是很准确。这里久介绍一款 LOC 大神的 PHP 版的基于solusVM面板 API 调用的VPS 流量统计页面。

图片[1] - #教程# – 基于solusVM面板API调用的VPS流量统计页面(PHP版本) - 云线路

PHP 代码

把下面的代码另存为status.php

<!DOCTYPE html>
<head>
  <title>服务器流量状态</title>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .table {
      width: 100%;
    }

    .table th,
    .table td {
      vertical-align: middle;
    }

    .progress {
      height: 20px;
    }
  </style>
</head>

<body>
  <div style="width: 90%; max-width: 800px; margin: 0 auto;">
    <h1>服务器流量状态</h1>
    <hr />

    <?php
    $lists = array(
      array("XXXXX-XXXXX-XXXXX", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "https://cp.greencloudvps.com/", "LA-2222"),
      array("XXXXX-XXXXX-XXXXX", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "https://cp.greencloudvps.com/", "JP-2222"),
      array("XXXXX-XXXXX-XXXXX", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "https://nerdvm.racknerd.com/", "Racknerd"),
      array("XXXXX-XXXXX-XXXXX", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "https://manage.dedicontrol.com/", "DDP")
      // 多个可以拷贝上面,最后一个不加逗号,其他加即可
    );

    $result = array();

    foreach ($lists as $key) {
      $result[] = post($key[0], $key[1], $key[2], $key[3]);
    }

    function humanFileSize($size, $unit = "")
    {
      if ((!$unit && $size >= 1 << 30) || $unit == "GB")
        return number_format($size / (1 << 30), 2) . " GB";
      if ((!$unit && $size >= 1 << 20) || $unit == "MB")
        return number_format($size / (1 << 20), 2) . " MB";
      if ((!$unit && $size >= 1 << 10) || $unit == "KB")
        return number_format($size / (1 << 10), 2) . " KB";
      return number_format($size) . " bytes";
    }

    function post($keys, $hashs, $naserurls, $beizhu)
    {

      $postfields["key"] = $keys;
      $postfields["hash"] = $hashs;
      $postfields["action"] = "info";
      $masterurl = $naserurls;
      $postfields["status"] = "true";
      $postfields["hdd"] = "true";
      $postfields["mem"] = "true";
      $postfields["bw"] = "true";

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, "{$masterurl}/api/client/command.php");
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: "));
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

      // Execute the request

      $data = curl_exec($ch);

      $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

      if ($code != 200) {
        $return['error'] = 1;

        if ($code == 405) {
          $return['message'] = "Incorrect API credentials.";
          return $return;
        }

        $return['message'] = "Invalid status code.";

        return $return;
      }

      // Close the Curl handle

      curl_close($ch);

      if (!$data) {
        $return['error'] = 1;
        $return['message'] = "Error connecting to API.";

        return $return;
      }

      // Extract the data

      preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', $data, $match);

      $result = array();
      $result['bz'] = $beizhu;
      foreach ($match[1] as $x => $y) {
        $result[$y] = $match[2][$x];
      }
      //获取出错
      if ($result['status'] == "error") {
        $result['error'] = 1;
        $result['message'] = $result['statusmsg'];

        return $result;
      }
      //获取正常
      $result['error'] = 0;

      $result['hdd'] = explode(",", $result['hdd']);
      $result['mem'] = explode(",", $result['mem']);
      $result['bw'] = explode(",", $result['bw']);
      return $result;
    }

    ?>

    <table class="table table-striped">
      <thead>
        <tr>
          <th>备注</th>
          <th>带宽</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($result as $key) { ?>
          <tr>
            <td><?php echo $key['bz']; ?></td>
            <td>
              <div class="progress">
                <div class="progress-bar <?php echo ($key['bw'][3] >= 90) ? 'bg-danger' : (($key['bw'][3] >= 75) ? 'bg-warning' : 'bg-success'); ?>" role="progressbar" style="width: <?php echo $key['bw'][3]; ?>%;" aria-valuenow="<?php echo $key['bw'][3]; ?>" aria-valuemin="0" aria-valuemax="100">
                  <?php echo $key['bw'][3]; ?>%
                </div>
              </div>
              <small>已用 <?php echo humanFileSize($key['bw'][1]); ?> / 总量 <?php echo humanFileSize($key['bw'][0]); ?></small>
            </td>
          </tr>
        <?php } ?>
      </tbody>
    </table>

    <a class="btn btn-primary" href="https://status.yunloc.com" role="button">返回</a>
  </div>
</body>

</html>

进入面板获取 API

图片[2] - #教程# – 基于solusVM面板API调用的VPS流量统计页面(PHP版本) - 云线路

结语

适用于 RN、greencloud、vir、ddp 等使用 solusVM 控制面板商家;
程序直接调用的 solusVM 面板的 API,获取代码参考网上现存的,用 GPT 美化了下;
这样就方便不用进每个小鸡面板分别查看了。

© 本站文章随意转载,但请注明出处!
THE END
点赞7 分享
评论 抢沙发
头像
务必使用真实的邮箱地址评论,虚假邮箱的评论将不通过审核及无回复。
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容