站点图标 Linux-技术共享

SolusVM API PHP Class Wrapper

SolusVM-PHP-API-Class

使用前提:先要进SolusVM后台开启API,得到key和hash

官方文档上提供的是POST方式提交数据,但是本人试过后总是报“417 – Expectation Failed”的错误,改用GET传数据后,就没有报错了

具体代码如下:

<?php

// Url to the client API
$url = “https://<MASTER IP>:5656/api/client”;

// Specify the key, hash and action

$postfields[“key”] = “EXAMPLE-API-KEY”;
$postfields[“hash”] = “exaMpleHasH”;
$postfields[“action”] = “reboot”; // reboot, shutdown, boot, status
// Send the query to the solusvm master

// Send the query to the solusvm master
// Note we have to use GET, POST seems to be broken.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . “/command.php?key=”.$postfields[‘key’].”&hash=”.$postfields[‘hash’].”&action=”.$postfields[‘action’].
“&ipaddr=true&bw=true&mem=true&hdd=true”);
// curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
curl_close($ch);

// Parse the returned data and build an array

preg_match_all(‘/<(.*?)>([^<]+)<\/\\1>/i’, $data, $match);
$result = array();
foreach ($match[1] as $x => $y)
{
$result[$y] = $match[2][$x];
}

echo “<pre>”;
print_r($result);
echo “</pre>”;
?>

SolusVM是很多VPS商家使用的一套VPS管理面板,针对客户提供了API功能以便管理账户下的VPS。

在SolusVM后台开启API功能,获取 API Key 和 API Hash(每台VPS对应不同的key和hash,如果账户下有多个机器,则需要分别获取

因为它的API允许直接使用GET方式访问,所以可以把其中信息替换掉,直接从浏览器打开就可以简单操作VPS

需要注意的是,有些商家的管理地址可能是http协议,访问时需注意。

https://SolusVM管理地址/api/client/command.php?key=KEY&hash=HASH&action=操作

其中可用的操作有:

SolusVM API PHP Class WrapperSolusVM-PHP-API-Class-Master    https://linuxword.com/wp-content/uploads/2022/05/SolusVM-PHP-API-Class-Master.zip
A PHP wrapper class for the SolusVM API which interacts with virtual servers.

version 1.2

Requires
PHP >= 7.4
Usage
First edit lines 11,12,13 in class.php for your hosting providers solusVM URL along with your account API key and API hash

Call the class file on any page that uses it

<?php
require_once('class.php');
Create a new instance on page of use:

$call = new solusClientApi();
Return all server info
$call->allInfo();
array

returns (edited certain values):

{
"status": "online",
"hostname": "myservershostname.com",
"location": "Los Angeles, CA, USA",
"type": "kvm",
"node": "KVM-1.LA.HOST.COM",
"ip_count": 2,
"ip_list": [
"111.222.333.444",
"111.222.333.555",
"111.222.333.666"
],
"mem_total": "512.0",
"mem_used": "220.0",
"mem_used_percent": 42,
"mem_data_type": "MB",
"bw_total": "512.0",
"bw_used": "0.0",
"bw_used_percent": 0,
"bw_data_type": "GB",
"hdd_total": "10.0",
"hdd_used": "5.0",
"hdd_used_percent": 50,
"hdd_data_type": "GB",
"datetime": "2020-04-04 15:56:29"
}
Get server status
$call->getStatus();
bool

IP address count
$call->ipCount();
int

Main ip address
$call->ipMain();
string

IP address from number in array created from list
$call->ip($number);
string

Server type (KVM, OVZ etc)
$call->type();
string

Server hostname
$call->hostname();
string

Server node
$call->node();
string

Server location
$call->location();
string

Reboot server
$call->reboot();
string

Boot server
$call->boot();
string

Shutdown server
$call->shutdown();
string

Total memory for server
$call->totalMem($convert_to, $decimals);
string

Memory available
$call->memAval($convert_to, $decimals);
string

Memory used percent
$call->memUsedPercent($decimals);
string

Memory free percent
$call->memFreePercent($decimals);
string

Total HDD for server
$call->totalHdd($convert_to, $decimals);
string

HDD available
$call->hddAval($convert_to, $decimals);
string

HDD used percent
$call->hddUsedPercent($decimals);
string

HDD free percent
$call->hddFreePercent($decimals);
string

Total bandwidth for server
$call->totalBw($convert_to, $decimals);
string

Bandwidth available
$call->bwAval($convert_to, $decimals);
string

Bandwidth used percent
$call->bwUsedPercent($decimals);
string

Bandwidth free percent
$call->bwFreePercent($decimals);
string

Set up reverse DNS
$call->rdns($ip, $rdns);
string

 

 

退出移动版