搬瓦工VPS支持API远程调用管理 提供PHP调用示范

我们在管理Bandwagonhost 搬瓦工VPS主机的时候看到在 KiwiVM控制面板中最下面还有提供 API 调用的接口,也就是说如果我们可以自定义通过外部的来管理VPS主机。在这篇文章中,老蒋准备分享看看在搬瓦工VPS主机中的API 的调用和使用。

如上图所示,我们可以看到 Your VEID 和 点击上面的 Show API Key 按钮后得到 Your API KEY的信息。

 Whether you want to automate nightly snapshots, monitor your VPS status, or even build a custom replacement for the KiwiVM panel, this page has you covered.

To export a CSV-formatted list of API keys for all instances under your account, use the CSV export feature in our billing portal.

All parameters can be passed using either the GET or POST methods. 

我们需要注意的,这个API的密钥信息和ID需要保管好,不要泄露 ,否则也可能会他人远端调用。

在这里,搬瓦工VPS官方也有推出使用API的示范。

PHP获取信息:

$request = "https://api.64clouds.com/v1/getServiceInfo?veid=2009256&api_key=YOUR_API_KEY_HERE";
$serviceInfo = json_decode (file_get_contents ($request));
print_r ($serviceInfo);

得到的结果:

/* ------------------------------- [ output ] -------------------------------
stdClass Object
(
    [hostname] => my.server.com
    [node_alias] => Node32
    [node_location] => US, Florida
    [plan] => micro128
    [plan_monthly_data] => 322122547200
    [plan_disk] => 4294967296
    [plan_ram] => 155189248
    [plan_swap] => 37748736
    [os] => centos-6-x86_64
    [email] => customer@example.com
    [data_counter] => 569810827
    [data_next_reset] => 1430193600
    [ip_addresses] => Array
        (
            [0] => 11.22.33.44
            [1] => 11.22.33.45
        )

    [rdns_api_available] => 1
    [ptr] => stdClass Object
        (
            [11.22.33.44] => ns1.my.server.com
            [11.22.33.45] => ns2.my.server.com
        )

    [error] => 0
)
*/

创建快照:

$request = "https://api.64clouds.com/v1/snapshot/create?description=Automatic_Snapshot&veid=2009256&api_key=YOUR_API_KEY_HERE";
$serviceInfo = json_decode (file_get_contents ($request));
print_r ($serviceInfo);

重启VPS

$request = "https://api.64clouds.com/v1/restart?veid=2009256&api_key=YOUR_API_KEY_HERE";
$serviceInfo = json_decode (file_get_contents ($request));
print_r ($serviceInfo);

用 wget调用:

wget -qO- "https://api.64clouds.com/v1/restart?veid=2009256&api_key=YOUR_API_KEY_HERE"

使用 CURL调用:

$requestData = array ("veid" => 2009256, "api_key" => "YOUR_API_KEY_HERE");
$request = "restart";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://api.64clouds.com/v1/$request"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // curl running on Windows has issues with SSL - 
                                             // see https://kb.ucla.edu/articles/how-do-i-use-curl-in-php-on-windows
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$jsonData = curl_exec($ch); 
if (curl_error($ch)) die("Connection Error: ".curl_errno($ch)." - ".curl_error($ch));
curl_close($ch);
print_r (json_decode ($jsonData));

您可能希望使用curl代替,因为它允许在POST请求中传递所有变量。

投上你的一票

本文出处:老蒋部落 » 搬瓦工VPS支持API远程调用管理 提供PHP调用示范 | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送