PHP WHOIS provides parsed and raw whois lookup of domains and ASN routes. PHP 5.4+ and 7+ compatible
Clone or download

README.md

PHP WHOIS

Build Status PHP version Packagist

PHP WHOIS client implementation. Sends the queries directly to the WHOIS services.

Use case

  • Raw and parsed domain lookup
  • Raw and parsed ASN routes lookup
  • Direct queries to TLD/ASN hosts
  • Extending and customizing the default hosts, parsers, etc.

Installation

System requirements:
  • PHP >= 5.4 (compatible with 7.* up to nightly)
  • php-intl
  • php-mbstring
  • php-memcached + Memcached server (both optional)
  • Allowed port 43 in firewall
Project requirements:
  • PSR-4 autoloader
Composer:
composer require io-developer/php-whois

or composer.json:

"require": {
    "io-developer/php-whois": "*"
}

Usage

Domain lookup

How to get summary about domain:
<?php

use Iodev\Whois\Whois;

// Creating default configured client
Whois::create();

// Checking availability
if (Whois::create()->isDomainAvailable("google.com")) {
    print "Bingo! Domain is available! :)";
}

// Supports Unicode (converts to punycode)
if (Whois::create()->isDomainAvailable("почта.рф")) {
    print "Bingo! Domain is available! :)";
}

// Getting raw-text lookup
$response = Whois::create()->lookupDomain("google.com");
print $response->getText();

// Getting parsed domain info
$info = Whois::create()->loadDomainInfo("google.com");
print_r([
    'Domain created' => date("Y-m-d", $info->getCreationDate()),
    'Domain expires' => date("Y-m-d", $info->getExpirationDate()),
    'Domain owner' => $info->getOwner(),
]);
Exceptions on domain lookup:
<?php

use Iodev\Whois\Whois;
use Iodev\Whois\Exceptions\ConnectionException;
use Iodev\Whois\Exceptions\ServerMismatchException;
use Iodev\Whois\Exceptions\WhoisException;

try {
    $info = Whois::create()->loadDomainInfo("google.com");
    if (!$info) {
        print "Null if domain available";
        exit;
    }
    print $info->getDomainName() . " expires at: " . date("d.m.Y H:i:s", $info->getExpirationDate());
} catch (ConnectionException $e) {
    print "Disconnect or connection timeout";
} catch (ServerMismatchException $e) {
    print "TLD server (.com for google.com) not found in current server hosts";
} catch (WhoisException $e) {
    print "Whois server responded with error '{$e->getMessage()}'";
}
TLD hosts customization:
<?php

use Iodev\Whois\Whois;
use Iodev\Whois\Modules\Tld\TldServer;
use Iodev\Whois\Modules\Tld\TldParser;

$whois = Whois::create();

// Define custom whois host
$customServer = new TldServer(".custom", "whois.nic.custom", false, TldParser::create());

// Or define the same via assoc way
$customServer = TldServer::fromData([
    "zone" => ".custom",
    "host" => "whois.nic.custom",
]);

// Add custom server to existing whois instance
$whois->getTldModule()->addServers([$customServer]);

// Now it can be utilized
$info = $whois->loadDomainInfo("google.custom");

var_dump($info);
TLD default/fallback servers:
<?php

use Iodev\Whois\Whois;
use Iodev\Whois\Modules\Tld\TldServer;

$whois = Whois::create();

// Add default servers
$matchedServers = $whois->getTldModule()
    ->addServers(TldServer::fromDataList([
        ['zone' => '.*.net', 'host' => 'localhost'],
        ['zone' => '.uk.*', 'host' => 'localhost'],
        ['zone' => '.*', 'host' => 'localhost'],
    ]))
    ->matchServers('some.uk.net');

foreach ($matchedServers as $s) {
    echo "{$s->getZone()}  {$s->getHost()}\n";
}

// Matched servers + custom defaults:
//
// .uk.net  whois.centralnic.com
// .uk.net  whois.centralnic.net
// .uk.*  localhost
// .*.net  localhost
// .net  whois.crsnic.net
// .net  whois.verisign-grs.com
// .*  localhost

ASN lookup

How to get summary using ASN number:
<?php

use Iodev\Whois\Whois;

// Getting raw-text lookup
$response = Whois::create()->lookupAsn("AS32934");
print $response->getText();

// Getting parsed ASN info
$info = Whois::create()->loadAsnInfo("AS32934");
foreach ($info->getRoutes() as $route) {
    print_r([
        'route IPv4' => $route->getRoute(),
        'route IPv6' => $route->getRoute6(),
        'description' => $route->getDescr(),
    ]);   
}

Response caching

Some TLD hosts are very limited for frequent requests. Use cache if in your case requests are repeating.

<?php

use Iodev\Whois\Whois;
use Iodev\Whois\Loaders\SocketLoader;
use Iodev\Whois\Loaders\MemcachedLoader;

$m = new Memcached();
$m->addServer('127.0.0.1', 11211);
$loader = new MemcachedLoader(new SocketLoader(), $m);

$whois = Whois::create($loader);

Contributing

The project is open for pull requests, issues and feedback. Please read the CODE_OF_CONDUCT.md