webinarium/linode-api3

PHP Linode API v3 客户端库

1.1.15 2018-07-15 08:03 UTC

This package is auto-updated.

Last update: 2024-09-09 14:25:41 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