arodygin/linode-api-php

该软件包已被废弃且不再维护。作者建议使用webinarium/linode-api3软件包。

PHP版的Linode API v3客户端库

1.1.15 2018-07-15 08:03 UTC

README

PHP Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality

PHP版的Linode API v3客户端库

此库实现了Linode API v3的全功能规范(符合https://www.linode.com/api/utility/api.spec),包括在Linode网站上尚未描述的功能(目前文档似乎有些过时)。

此库并非实际编写,而是从规范自动生成的。这种方法提供了几个优点,例如

  • 我们可以确信规范中没有遗漏任何内容,
  • 没有因人为因素可能引起的实现错误,
  • 如果在规范扩展的情况下,快速且轻松地更新库代码。

请注意,“test.echo”在实现中已被跳过。

警告

此库针对Linode的旧版API。对于最新API,请参阅此库

要求

PHP需要至少版本PHP 5.6。

安装

推荐的安装方法是使用Composer

composer require "webinarium/linode-api3"

使用示例

以下是如何使用此库创建新的Linode主机的一个完整示例

use Linode\LinodeApi;
use Linode\LinodeException;
use Linode\PaymentTerm;

// Your API key from the Linode profile.
$key = '...';

// Hardcode some IDs to make the example shorter.
// Normally you might want to use "Linode\AvailApi" class functions.
$datacenter = 3;    // Fremont datacenter
$plan       = 1;    // we will use the cheapest plan

// Create new linode.
try {
    $api = new LinodeApi($key);
    $res = $api->create($datacenter, $plan, PaymentTerm::ONE_MONTH);

    printf("Linode #%d has been created.\n", $res['LinodeID']);
}
catch (LinodeException $e) {
    printf("Error #%d: %s.\n", $e->getCode(), $e->getMessage());
}

批量请求

Linode API还支持批量模式,您提供多个请求集,并返回一个响应数组。以下使用此库的批量请求示例

use Linode\Batch;
use Linode\LinodeApi;
use Linode\PaymentTerm;

// Your API key from the Linode profile.
$key = '...';

// Hardcode some IDs to make the example shorter.
// Normally you might want to use "Linode\AvailApi" class functions.
$datacenters = [2, 3, 4, 6];    // all four US datacenters
$plan        = 1;               // we will use the cheapest plan

// Create a batch.
$batch = new Batch($key);

// Create new linode on each of US datacenters.
$api = new LinodeApi($batch);

foreach ($datacenters as $datacenter) {
    $api->create($datacenter, $plan, PaymentTerm::ONE_MONTH);
}

// Execute batch.
$results = $batch->execute();

foreach ($results as $res) {
    printf("Linode #%d has been created.\n", $res['DATA']['LinodeID']);
}

测试

几乎所有测试都是模拟的,因此您不需要使用真实的API密钥,也不会影响任何真实的Linode。唯一进行完整API调用的测试是TestTest(用于"test.echo")和ApiTest(用于"api.spec")

./bin/phpunit --coverage-text

库再生

如果您想重新生成库代码,可以简单地分两步进行

php ./generator/generator
php ./bin/php-cs-fixer fix