Skip to content

Latest commit

 

History

History
77 lines (56 loc) · 2.2 KB

README.en.md

File metadata and controls

77 lines (56 loc) · 2.2 KB

Cloud.ru DNS API client

Latest Stable Version Total Downloads

The package provides a PHP wrapper to interact with the Cloud.ru DNS API.

Requirements

  • PHP 8.2 or higher.

Installation

composer require anktx/cloud-dns-client

General usage

To interact with the Cloud.ru DNS API, you need to create an instance of the CloudDnsApi class. This class requires a PSR-18 ClientInterface implementation and HttpAdapter, which in turn requires PSR-17 RequestFactoryInterface and StreamFactoryInterface.

You can use the kriswallsmith/buzz and nyholm/psr7 packages for this:

composer require kriswallsmith/buzz nyholm/psr7

Here's now you can create an instance of CloudDnsApi:

use Anktx\Cloud\Dns\Client\Client\HttpAdapter;
use Anktx\Cloud\Dns\Client\CloudDnsApi;
use Buzz\Client\Curl;
use Nyholm\Psr7\Factory\Psr17Factory;

// Dependencies
$psr17Factory = new Psr17Factory();
$httpAdapter = new HttpAdapter($psr17Factory, $psr17Factory);
$httpClient = new Curl($psr17Factory);

// API
$api = new CloudDnsApi(
    client: $httpClient,
    httpAdapter: $httpAdapter,
);

First, obtain an authentication token and pass it to HttpAdapter:

$token = $api->authenticate('CLIENT_ID', 'CLIENT_SECRET');
$httpAdapter->setToken($token);

Now you can use the $api instance to interact with the Cloud.ru DNS API.

// Get zones
$api->getZones('PROJECT_ID');

// Create zone
$api->createZone('New zone', 'PROJECT_ID');

The result will be either FailResult instance (on error) or an object of the corresponding type (on success). For example:

// Result is a collection of `Record` objects
$records = $api->getRecords('ZONE_ID');

foreach ($records as $record) {
    echo 'name: ' . $record->name . ' ttl: ' . $record->ttl . \PHP_EOL;
}