搬瓦工 KiwiVM 教程:使用 PHP 调用 REST API 编程

搬瓦工 KiwiVM 控制面板会提供一个 API 接口,我们可以根据提供的 API,获取一下 API KEY,然后就可以使用 PHP、Python、或者 shell 等编程语言进行编程,来调用 API 接口,进行一些操作,比如获取 VPS 信息、重启 VPS、迁移机房等。

本文介绍一下搬瓦工 API KEY 获取方法,然后给一段 PHP 示例代码,最后简单介绍一下 API 返回信息。

一、获取搬瓦工 API KEY

登录搬瓦工 KiwiVM 控制面板:《搬瓦工新手教程:购买之后登录网站后台/KiwiVM 控制面板进行管理方法》。

左侧菜单的 API 就是搬瓦工 KiwiVM 控制面板的 API 界面,点击之后,就可以看到 REST API 的 API KEY,如图所示。需要点击一下 Show API Key 才会显示 API KEY,或者点击 Reset API Key 就可以重置 API。

介绍文字:

Whether you want to automatically create nightly snapshots, query status of your VPS, or develop a complete replacement for the KiwiVM panel, this page is for you.

To export a csv-formatted list of API keys of all instances under your account, please use our billing portal’s CSV export feature.

All parameters can be passed either via GET or POST methods.

翻译如下:

无论您是想自动创建夜间快照、查询 VPS 状态,还是开发 KiwiVM 面板的完整替代品,此页面都适合您。

要导出您账户下所有实例的 API 密钥的 csv 格式列表,请使用我们的账单门户的 CSV 导出功能。

所有参数都可以通过 GET 或 POST 方法传递。

二、PHP 调用 API 示例代码

搬瓦工 API 页面还有一些 PHP 示例代码,本文以第一个示例代码为例,简单介绍一下如何通过搬瓦工 API 获取 VPS 信息。

PHP 代码(将 YOUR_VEID_HEREYOUR_API_KEY_HERE 进行替换):

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

返回结果:

/* ------------------------------- [ output ] -------------------------------
stdClass Object
(
    [hostname] => my.server.com
    [node_ip] => 10.20.30.40
    [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
)
*/

三、API 返回参数解读

下面再简单介绍一下返回信息。

Each API call requires that you supply a valid combination of VEID (VPS ID) and API key as shown in the example.

Each API call always returns error variable. If error is non-zero, check “message” variable for more details about the error.

翻译如下:

每个 API 调用都要求您提供 VEID (VPS ID) 和 API 密钥的有效组合,如示例中所示。

每个 API 调用总是返回错误变量。 如果错误不为零,请检查“消息”变量以获取有关错误的更多详细信息。

以 getServiceInfo 为例,返回信息如下:

  • vm_type: Hypervizor type (ovz or kvm)
  • hostname: Hostname of the VPS
  • node_ip: IP address of the physical node
  • node_alias: Internal nickname of the physical node
  • node_location: Physical location (country, state)
  • location_ipv6_ready: Whether IPv6 is supported at the current location
  • plan: Name of plan
  • plan_disk: Disk quota (bytes)
  • plan_ram: RAM (bytes)
  • plan_swap: SWAP (bytes)
  • os: Operating system
  • email: Primary e-mail address of the account
  • plan_monthly_data: Allowed monthly data transfer (bytes). Needs to be multiplied by monthly_data_multiplier – see below.
  • data_counter: Data transfer used in the current billing month. Needs to be multiplied by monthly_data_multiplier – see below.
  • monthly_data_multiplier: Some locations offer more expensive bandwidth; this variable contains the bandwidth accounting coefficient.
  • data_next_reset: Date and time of transfer counter reset (UNIX timestamp)
  • ip_addresses: IPv4 and IPv6 addresses assigned to VPS (Array)
  • private_ip_addresses: Private IPv4 addresses assigned to VPS (Array)
  • ip_nullroutes: Information on IP address nullrouting during (D)DoS attacks (Array). Sample output when IP is under attack:
  • iso1: Mounted image #1
  • iso2: Mounted image #2 (currently unsupported)
  • available_isos: Array of ISO images available for use
  • plan_max_ipv6s: Maximum number of IPv6 addresses allowed by plan
  • rdns_api_available: Whether or not rDNS records can be set via API
  • plan_private_network_available: Whether or not Private Network features are available on this plan
  • location_private_network_available: Whether or not Private Network features are available at this location
  • ptr: rDNS records (Array of two-dimensional arrays: ip=>value)
  • suspended: Whether VPS is suspended
  • policy_violation: Whether there is an active policy violation that needs attention (see getPolicyViolations)
  • suspension_count: Number of times service was suspended in current calendar year
  • total_abuse_points: Total abuse points accumulated in current calendar year
  • max_abuse_points: Maximum abuse points allowed by plan in a calendar year

四、搬瓦工新手优惠推荐

搬瓦工官网: bwh81.net

1、搬瓦工实时库存监控

搬瓦工实时库存:https://stock.bwg.net

2、搬瓦工最推荐套餐

  1. 搬瓦工优惠码:BWHCGLUKKB
  2. 搬瓦工购买教程:《搬瓦工新手用户注册和支付宝购买图文指导教程
方案内存CPU硬盘流量/月带宽机房价格购买
KVM
(最便宜)
1GB2核20GB1TB1GbpsDC3 CN2
DC8 ZNET
(购买后在后台迁移)
$49.99/年购买
KVM2GB3核40GB2TB1Gbps$52.99/半年
$99.99/年
购买
CN2 GIA-E
(最推荐)
1GB2核20GB1TB2.5GbpsDC6 CN2 GIA-E
DC9 CN2 GIA
日本软银 JPOS_1
荷兰联通 EUNL_9
加拿大 CN2 GIA
$49.99/季度
$169.99/年
购买
CN2 GIA-E2GB3核40GB2TB2.5Gbps$89.99/季度
$299.99/年
购买
HK2GB2核40GB0.5TB1Gbps中国香港 CN2 GIA
新加坡 CN2 GIA
日本东京 CN2 GIA
日本大阪 CN2 GIA
$89.99/月
$899.99/年
购买
HK4GB4核80GB1TB1Gbps$155.99/月
$1559.99/年
购买
OSAKA2GB2核40GB0.5TB1.5Gbps日本大阪 CN2 GIA$49.99/月
$499.99/年
购买
OSAKA4GB4核80GB1TB1.5Gbps$86.99/月
$869.99/年
购买

选择建议:

  • 入门:洛杉矶 CN2 套餐,目前最便宜,可选 DC3 CN2/DC8 ZNET 等机房,CN2 GT 线路,入门之选。
  • 推荐:洛杉矶 CN2 GIA-E 套餐,速度超快,可选 DC6 CN2 GIA-E/DC9 CN2 GIA/日本软银 JPOS_1/联通荷兰 EUNL_9 等机房,性价比最高。
  • 高端:香港 CN2 GIA 套餐,价格较高,但是速度、延迟、丢包等方面无可挑剔。
赞(0)
未经允许不得转载:搬瓦工中文笔记 » 搬瓦工 KiwiVM 教程:使用 PHP 调用 REST API 编程

登录

找回密码

注册