PHP code example of opusdns / php-client

1. Go to this page and download the library: Download opusdns/php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

opusdns / php-client example snippets


use OpusDNS\Client\Client;
use OpusDNS\Client\Config;
use OpusDNS\Client\Enum\PeriodUnit;
use OpusDNS\Client\Enum\RenewalMode;
use OpusDNS\Client\Model\ContactHandle;
use OpusDNS\Client\Model\DomainCreate;
use OpusDNS\Client\Model\DomainPeriod;
use OpusDNS\Client\Model\Nameserver;

$client = Client::create(Config::sandbox('opk_your_api_key'));

// Is the name available?
$check = $client->domain()->eppCheckDomain(['example.com']);
if (!$check->results[0]->available) {
    exit('taken');
}

// Register it for one year with an existing contact.
$domain = $client->domain()->createDomain(new DomainCreate(
    contacts: ['registrant' => [new ContactHandle('contact_01h45ytscbebyvny4gc8cr8ma2')]],
    name: 'example.com',
    period: new DomainPeriod(PeriodUnit::Y, 1),
    renewalMode: RenewalMode::EXPIRE,
    nameservers: [new Nameserver('ns1.example.net'), new Nameserver('ns2.example.net')],
));

echo $domain->expiresOn?->format('Y-m-d');

use OpusDNS\Client\Client;
use OpusDNS\Client\Config;

$config = new Config(
    apiKey: 'opk_...',
    baseUrl: Config::PRODUCTION_URL,      // or Config::SANDBOX_URL
    userAgent: 'my-billing-app/2.0',      // defaults to the client token
    headers: ['X-Request-Source' => 'billing'],
    timeout: 30.0,                        // seconds, applied when Client::create() builds Guzzle
    connectTimeout: 5.0,
    clientToken: 'my-billing-app/2.0',    // sent as X-OpusDNS-Client; '' omits the header
);

// Guzzle, built by the client
$client = Client::create($config);

// Any PSR-18 client with PSR-17 request and stream factories
$client = new Client($config, $psr18Client, $requestFactory, $streamFactory);

$domain = $client->domain()->getDomain('example.com');

$renewal = $client->domain()->renewDomain('example.com', new DomainRenewRequest(
    currentExpiryDate: new \DateTimeImmutable('2027-01-31T10:00:00Z'),
    period: new DomainPeriod(PeriodUnit::Y, 1),
));
echo $renewal->newExpiryDate->format('Y-m-d');

$client->domain()->transferDomain(new DomainTransferIn('example.org', RenewalMode::RENEW, authCode: 'epp-code'));

$client->domain()->updateDomain('example.com', new DomainUpdate(renewalMode: RenewalMode::RENEW));

$contact = $client->contact()->createContact(new ContactCreate(
    city: 'Berlin', country: 'DE', disclose: false, email: '[email protected]',
    firstName: 'Jane', lastName: 'Doe', phone: '+49.301234567', postalCode: '10115', street: 'Unter den Linden 1',
));

$zone = $client->dns()->getZone('example.com');
$client->dns()->patchZoneRrsets('example.com', new DnsZoneRrsetsPatchOps([
    new DnsRrsetPatchOp(PatchOp::UPSERT, new DnsRrsetPatch('www.example.com.', [['rdata' => '192.0.2.1']], 3600, DnsRrsetType::A)),
]));

$page = $client->domain()->getDomains(search: 'example', expiresIn30Days: true, pageSize: 50);

use OpusDNS\Client\Paginator;

$expiring = Paginator::items(fn (int $page) => $client->domain()->getDomains(page: $page, pageSize: 100, expiresIn30Days: true));

foreach ($expiring as $domain) {
    echo $domain->name, ' expires ', $domain->expiresOn?->format('Y-m-d'), "\n";
}

foreach (Paginator::pages(fn (int $page) => $client->dns()->listZones(page: $page), maxPages: 5) as $number => $page) {
    echo "page {$number} of {$page->pagination()->totalPages}\n";
}

use OpusDNS\Client\Exception\ConflictException;
use OpusDNS\Client\Exception\HttpException;
use OpusDNS\Client\Exception\NetworkException;
use OpusDNS\Client\Exception\ValidationException;

try {
    $client->domain()->createDomain($request);
} catch (ValidationException $e) {
    foreach ($e->messages() as $message) {
        echo $message, "\n";
    }
} catch (ConflictException $e) {
    echo $e->problemDetail;
} catch (HttpException $e) {
    echo $e->statusCode(), ' ', $e->problemTitle;
} catch (NetworkException $e) {
    // retry later
}

use OpusDNS\Client\Config;
use OpusDNS\Client\Testing\FakeHttpClient;

$http = new FakeHttpClient();
$client = $http->client(Config::sandbox('test-key'));

$http->queueJson(200, ['name' => 'example.com', 'roid' => 'D1-OPUS', 'sld' => 'example', 'tld' => 'com']);
$domain = $client->domain()->getDomain('example.com');

$http->lastRequest()->getUri();   // https://sandbox.opusdns.com/v1/domains/example.com
$http->requests;                  // every request sent so far

use OpusDNS\Client\Endpoint;
use OpusDNS\Client\Permission;

$response = $client->request('GET', Endpoint::DOMAINS_BY_DOMAIN_REFERENCE, path: ['domain_reference' => 'example.com']);
$data = $client->decodeArray($response);

Permission::
bash
composer 
bash
php examples/register-domain.php
php examples/renew-domain.php opusdns-php-client-1234abcd.com
php examples/update-domain.php opusdns-php-client-1234abcd.com