io-developer/php-whois

PHP WHOIS 提供域和 ASN 路由的解析和原始 whois 查询。兼容 PHP 5.4+ 和 7+

4.1.10 2023-01-25 14:42 UTC

README

PHP version Packagist

PHP WHOIS 客户端实现。直接向 WHOIS 服务发送查询。

使用场景

  • 原始和解析域名查找
  • 原始和解析 ASN 路由查找
  • 直接查询 TLD/ASN 主机
  • 扩展和自定义默认主机、解析器等
  • 通过 CurlLoader 代理

安装

系统要求
  • PHP >= 7.2(旧版本支持 5.4+
  • php-curl
  • php-mbstring
  • 在防火墙中打开端口 43

可选

  • php-intl
  • php-memcached + Memcached 服务器
项目要求
  • PSR-4 自动加载器
Composer
composer require io-developer/php-whois

或 composer.json

"require": {
    "io-developer/php-whois": "^4.0"
}

使用方法

域名查找

如何获取域的摘要信息
<?php

use Iodev\Whois\Factory;

// Creating default configured client
$whois = Factory::get()->createWhois();

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

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

// Getting raw-text lookup
$response = $whois->lookupDomain("google.com");
print $response->text;

// Getting parsed domain info
$info = $whois->loadDomainInfo("google.com");
print_r([
    'Domain created' => date("Y-m-d", $info->creationDate),
    'Domain expires' => date("Y-m-d", $info->expirationDate),
    'Domain owner' => $info->owner,
]);
域名查找异常
<?php

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

try {
    $whois = Factory::get()->createWhois();
    $info = $whois->loadDomainInfo("google.com");
    if (!$info) {
        print "Null if domain available";
        exit;
    }
    print $info->domainName . " expires at: " . date("d.m.Y H:i:s", $info->expirationDate);
} 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()}'";
}
使用 SOCKS5 代理
<?php

use Iodev\Whois\Loaders\CurlLoader;
use Iodev\Whois\Factory;

$loader = new CurlLoader();
$loader->replaceOptions([
    CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5,
    CURLOPT_PROXY => "127.0.0.1:1080",
    //CURLOPT_PROXYUSERPWD => "user:pass",
]);
$whois = Factory::get()->createWhois($loader);

var_dump([
    'ya.ru' => $whois->loadDomainInfo('ya.ru'),
    'google.de' => $whois->loadDomainInfo('google.de'),
]);
TLD 主机自定义
<?php

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

$whois = Factory::get()->createWhois();

// Define custom whois host
$customServer = new TldServer(".custom", "whois.nic.custom", false, Factory::get()->createTldParser());

// 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 默认/回退服务器
<?php

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

$whois = Factory::get()->createWhois();

// 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 查找

如何使用 ASN 编号获取摘要
<?php

use Iodev\Whois\Factory;

$whois = Factory::get()->createWhois();

// Getting raw-text lookup
$response = $whois->lookupAsn("AS32934");
print $response->text;

// Getting parsed ASN info
$info = $whois->loadAsnInfo("AS32934");
foreach ($info->routes as $route) {
    print_r([
        'route IPv4' => $route->route,
        'route IPv6' => $route->route6,
        'description' => $route->descr,
    ]);   
}

响应缓存

一些 TLD 主机对频繁请求限制很大。如果您的请求重复,请使用缓存。

<?php

use Iodev\Whois\Factory;
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 = Factory::get()->createWhois($loader);
// do something...

开发

支持的 PHP 版本配置在 docker-compose.yml

常见使用场景

  1. 设置并运行所有测试:docker compose up --build
  2. 在特定 PHP 版本下运行测试:docker compose up php-8.2_intl --build
  3. 运行脚本:docker compose run php-8.2_intl bin/php-whois info google.com

另请参阅 TESTS.md

贡献

该项目接受拉取请求、问题和反馈。请阅读 CODE_OF_CONDUCT.md