whois 查询域名注册信息,可以使用 whois.com 和 who.is 等在线工具,也可以使用命令行工具 whois:windows 有 sysinternal 版 whois.exe,linux 也可以安装 whois。
不过,很多时候,大家可能不想使用第三方工具,使用自己的工具更开心更放心。
whois 协议并不复杂,稍微写几行代码就可以自己写出一个 whois 自定义查询程序。
以下代码简单实现了纯 PHP 实现 whois 查询域名注册信息,可以按需要扩展它的功能提升它的健壮性。- <?php
- /**
- * 纯 PHP 实现 whois 查询域名注册信息
- * @author 吴先成
- * @link qsez.org
- * @link wuxiancheng.cn
- * @param string $domain 要查询的域名
- * @return string whois 服务器提供的域名信息
- * @throws Exception 查询无效域名或无法连接 whois 服务器时抛出
- */
- function whois(string $domain){
- if(false === strpos($domain, '.')){
- throw new Exception('invalid domain', 10086);
- }
- $domainParts = explode(".", $domain);
- $domainExtension = array_pop($domainParts);
- $server = "{$domainExtension}.whois-servers.net";
- $fp = fsockopen($server, 43, $errorCode, $errorMessage, 9);
- if(false === $fp){
- throw new Exception($errorMessage, $errorCode);
- }
- fwrite($fp, "$domain\r\n");
- $responseText = '';
- while(!feof($fp)){
- $responseText .= fgets($fp, 1024 * 1024);
- }
- return $responseText;
- }
- try{
- echo whois('wuxiancheng.cn');
- }catch(Exception $e){
- echo $e->getMessage();
- }
复制代码 参考链接 whois 协议 |
|