webinarium/linode-api3

Linode API v3 Client Library for PHP

1.1.14 2017-10-07 05:58 UTC

README

PHP Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality

Linode API v3 Client Library for PHP

This library implements full spec of Linode API v3 (in accordance with https://www.linode.com/api/utility/api.spec), including functions which are not described at the Linode's site yet (the documentation seems to be slightly outdated at the moment).

The library wasn't actually implemented, but autogenerated from the spec. This approach provides several advantages as:

  • we can be sure that nothing from the spec is missed,
  • no implementation errors which could be caused by human factor,
  • in case of the spec extension it's fast and easy to update the library's code.

Also please note that "test.echo" is skipped from the implementation.

Requirements

PHP needs to be a minimum version of PHP 5.6.

Installation

The recommended way to install is via Composer:

composer require "webinarium/linode-api3"

Usage Example

Below is a complete example of how to create a new Linode host using the library:

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());
}

Batching Requests

The Linode API also supports a batched mode, whereby you supply multiple request sets and receive back an array of responses. Example batch request using the library:

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']);
}

Tests

Almost all tests are mocked so you don't have to use a real API key and no real linodes are affected. The only tests which make a complete API call are TestTest (for "test.echo") and ApiTest (for "api.spec"):

./bin/phpunit --coverage-text

Library regeneration

If you would like to regenerate the library code, you can do it with two simple steps:

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