找回密码
 新建账号

纯 PHP 实现 whois 查询域名注册信息

[复制链接]
大郎 发表于 2025-12-21 15:06 | 显示全部楼层 |阅读模式
whois 查询域名注册信息,可以使用 whois.com 和 who.is 等在线工具,也可以使用命令行工具 whois:windows 有 sysinternal 版 whois.exe,linux 也可以安装 whois。

不过,很多时候,大家可能不想使用第三方工具,使用自己的工具更开心更放心。

whois 协议并不复杂,稍微写几行代码就可以自己写出一个 whois 自定义查询程序。

以下代码简单实现了纯 PHP 实现 whois 查询域名注册信息,可以按需要扩展它的功能提升它的健壮性。
  1. <?php
  2.     /**
  3.      * 纯 PHP 实现 whois 查询域名注册信息
  4.      * @author 吴先成
  5.      * @link qsez.org
  6.      * @link wuxiancheng.cn
  7.      * @param string $domain 要查询的域名
  8.      * @return string whois 服务器提供的域名信息
  9.      * @throws Exception 查询无效域名或无法连接 whois 服务器时抛出
  10.      */
  11.     function whois(string $domain){
  12.         if(false === strpos($domain, '.')){
  13.             throw new Exception('invalid domain', 10086);
  14.         }
  15.         $domainParts = explode(".", $domain);
  16.         $domainExtension = array_pop($domainParts);
  17.         $server = "{$domainExtension}.whois-servers.net";
  18.         $fp = fsockopen($server, 43, $errorCode, $errorMessage, 9);
  19.         if(false === $fp){
  20.             throw new Exception($errorMessage, $errorCode);
  21.         }
  22.         fwrite($fp, "$domain\r\n");
  23.         $responseText = '';
  24.         while(!feof($fp)){
  25.             $responseText .= fgets($fp, 1024 * 1024);
  26.         }
  27.         return $responseText;
  28.     }

  29.     try{
  30.         echo whois('wuxiancheng.cn');
  31.     }catch(Exception $e){
  32.         echo $e->getMessage();
  33.     }
复制代码
参考链接 whois 协议

手机版|轻松e站

GMT+8, 2026-1-27 02:58

快速回复 返回顶部 返回列表