sandwave-io/dns-made-easy

PHP SDK for the DNS Made Easy API

0.2.1 2023-05-09 10:02 UTC

This package is auto-updated.

Last update: 2024-10-10 13:52:39 UTC


README

DNS Made Easy PHP Client Library

Codecov GitHub Workflow Status Packagist PHP Version Support Packagist PHP Version Support Packagist Downloads

This package is a fork from the original DNSMadeEasy/dme-php-sdk.

This is an API client library for the DNS Made Easy API.

More information about the API may be found in the official API documentation.

Installation

The easiest way to install and use this client library is using Composer. The following command will add the library to your application and install it from Packagist.

composer require sandwave-io/dns-made-easy

Getting Started

You will need a DNS Made Easy account and API credentials. You can get an account at the DNS Made Easy website. There is an API sandbox available, you can create a new account here.

With the package installed through composer, you just need to create the client and set the API key and secret key.

$client = new \DnsMadeEasy\Client;
$client->setApiKey(API_KEY);
$client->setSecretKey(SECRET_KEY);

You may now use the client to query the API retrieve objects. Usage is documented in GitHub in the docs directory.

Using the Sandbox

You can tell the client to use the sandbox API endpoint by using the setEndpoint method:

$client->setEndpoint('https://api.sandbox.dnsmadeeasy.com/V2.0');

Putting it all together

Putting this together, it's time for the API equivalent of Hello World. Let's get a list of your domains.

<?php

// Create a new client and set our credentials
$client = new \DnsMadeEasy\Client;
$client->setApiKey("Your API Key");
$client->setSecretKey("Your Secret Key");

// Configure it to use the Sandbox
$client->setEndpoint('https://api.sandbox.dnsmadeeasy.com/V2.0');

// Create a new domain
$domain = $client->domains->create();
$domain->name = 'mydomain.example.com';
$domain->save();

// Print out our domain
echo json_encode($domain, JSON_PRETTY_PRINT);

// Now fetch a list of our domains
$domains = $client->domains->paginate();
foreach ($domains as $domain) {
    echo json_encode($domain, JSON_PRETTY_PRINT);
}

There are more examples further down of using the API client SDK.

Configuration

There are additional configuration options you can use with the client as well as just specifying the sandbox.

Logging

You can specify a logger that implements the PSR-3 Logger specification such as MonoLog. The client is a LoggerAwareInterface and the logger can be specified either in the constructor or via a method call.

$client = new \DnsMadeEasy\Client(null, null, $myLogger);
$client->setLogger($myLogger);

If no logger is specified then a null logger that does nothing will be used.

Custom HTTP Client

If you need additional configuration for HTTP requests in your application, for example to specify a proxy server or if you want to use your own HTTP client matching the PSR-18 HTTP Client specification.

You can specify the client using either the constructor or via a method call.

$client = new \DnsMadeEasy\Client($myClient);
$client->setHttpClient($myClient);

Examples

Full documentation of the library methods are in the docs folder.

Managers

Managers are used for managing your access to resources on the API, including creating new resources and fetching existing ones from the API. These can be accessed as properties on the client.

// Fetch our manager
$domainsManager = $client->domains;
// Ask our manager for the domain
$domain = $domainsManager->get(1234);

Manages are also used to create new objects.

// Create a new domain
$domain = $client->domains->create();
$domain->name = 'example.com';

// Save the domain
$domain->save();

The domain is not saved on the API until you call $domain->save().

Multiple objects can be fetched using the paginate() method on the manager. You can specify the page number and the number of items per page.

// Return the 4th page of results with the default page size
$client->domains->paginate(4);

// Return the first page of 50 results
$client->domains->paginate(1, 50);

Models

The models themselves follow an Active Record pattern. Properties can be updated and save() called on the model to update the API.

// Fetch an existing domain with the ID 1234
$domain = $client->domains->get(1234);
// Update the gtdEnabled property
$domain->gtdEnabled = true;
// Save the domain object on the API
$domain->save();

You can delete an object by calling delete() on it:

$domain = $client->domains->get(1234);
$domain->delete();

Creating a domain and records

This example creates a new domain and adds records to it.

// Create the client
$client = new \DnsMadeEasy\Client;
$client->setApiKey(API_KEY);
$client->setSecretKey(SECRET_KEY);

// Create the domain
$domain = $client->domains->create();
$domain->name = 'example.com';
$domain->save();

// Create a record on the domain
$record = $domain->records->create();
$record->type = \DnsMadeEasy\Enums\RecordType::A;
$record->name = 'www';
$record->value = '192.0.2.1';
$record->save();

// Get a list of all domains
$domains = $client->domains->paginate();
foreach ($domains as $domain) {
    print_r(json_encode($domain, JSON_PRETTY_PRINT));
}

Contributing

When contributing to this project, you can run the following quality checks:

$ vendor/bin/phpstan
$ vendor/bin/phpunit
$ vendor/bin/php-cs-fixer fix