Skip to content

Repository files navigation

Utopia Domains

Important

This repository is a read-only mirror of the utopia-php monorepo. Development happens in packages/domains — please open issues and pull requests there.

Total downloads Discord

Utopia Domains parses domain names using the Public Suffix List. It can identify a domain's suffix, registerable name, and subdomain. Registrar adapters provide domain registration operations through OpenSRS and Name.com.

Installation

Install the package with Composer:

composer require utopia-php/domains

Utopia Domains requires PHP 8.5 or later with the cURL, mbstring, and SimpleXML extensions.

Domain parsing

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Utopia\Domains\Domain;

$domain = new Domain('demo.example.co.uk');

$domain->get();             // demo.example.co.uk
$domain->getTLD();          // uk
$domain->getSuffix();       // co.uk
$domain->getRegisterable(); // example.co.uk
$domain->getName();         // example
$domain->getSub();          // demo
$domain->isKnown();         // true
$domain->isICANN();         // true
$domain->isPrivate();       // false
$domain->isTest();          // false

For a URL, extract its host before creating the domain:

$host = parse_url('https://www.example.com/path', PHP_URL_HOST);
$domain = new Domain($host);

The parser exposes these methods:

  • get() returns the complete domain name.
  • getTLD() returns the top-level domain.
  • getSuffix() returns the matching public suffix.
  • getRegisterable() returns the public suffix plus its preceding label.
  • getName() returns the registerable domain name without its suffix.
  • getSub() returns the subdomain.
  • isKnown() reports whether the suffix exists in the dataset.
  • isICANN() reports whether the suffix belongs to the ICANN section.
  • isPrivate() reports whether the suffix belongs to the private section.
  • isTest() reports whether the top-level domain is localhost or test.

The generated dataset lives in data/data.php. Maintainers can refresh it with:

php data/import.php

Registrar adapters

Create an adapter for the registrar and pass it to Registrar. Default nameservers belong to Registrar, not the adapter.

OpenSRS adapter

use Utopia\Domains\Registrar;
use Utopia\Domains\Registrar\Adapter\OpenSRS;

$adapter = new OpenSRS(
    'api-key',
    'username',
    'password',
    'https://horizon.opensrs.net:55443',
);

$registrar = new Registrar($adapter, [
    'ns1.nameserver.com',
    'ns2.nameserver.com',
]);

Use https://rr-n1-tor.opensrs.net:55443 as the OpenSRS production endpoint.

Name.com

use Utopia\Domains\Registrar;
use Utopia\Domains\Registrar\Adapter\NameCom;

$adapter = new NameCom(
    'username',
    'api-token',
    'https://api.name.com',
);

$registrar = new Registrar($adapter, [
    'ns1.name.com',
    'ns2.name.com',
]);

Name.com's sandbox endpoint is https://api.dev.name.com.

Registrar operations

use Utopia\Domains\Registrar\Contact;
use Utopia\Domains\Registrar\UpdateDetails;

$contact = new Contact(
    'First',
    'Last',
    '+1.5555555555',
    'person@example.com',
    '123 Example Street',
    '',
    '',
    'Example City',
    'CA',
    'US',
    '12345',
    'Example Inc.',
);

$available = $registrar->available('example.com');
$orderId = $registrar->purchase('example.com', $contact, 1);
$suggestions = $registrar->suggest(['example'], ['com', 'net'], 10);
$details = $registrar->getDomain('example.com');
$renewal = $registrar->renew('example.com', 1);
$transferOrderId = $registrar->transfer('example.com', 'auth-code');
$registrar->updateDomain('example.com', new UpdateDetails(autoRenew: true));

The registrar API also provides tlds(), updateNameservers(), getPrice(), getAuthCode(), cancelPurchase(), and checkTransferStatus().

License

Utopia Domains is available under the MIT License.

About

Lite & fast micro PHP domain parsing library that is **easy to use**.

Topics

Resources

Code of conduct

Contributing

Stars

56 stars

Watchers

10 watching

Forks

Releases

Packages

Used by

Contributors

Languages