PHP中强大的域Whois查询
Whois查询将告诉您一些有关域名的信息。尽管在Windows系统上不是默认设置,但您可以键入:
whoisgoogle.com
在大多数Linux安装中,请查看有关该google.com域的一些信息。您看到的信息取决于您正在查看的域以及顶级域(TLD)所采用的规则。有关whois的更多信息,请查看有关该主题的Wikipedia条目。
为了在PHP中获得此信息,您将需要询问适当的TLDwhois服务器以获取有关域的信息。但是,您从哪里获得服务器列表?根区域数据库具有完整的域列表,通过单击TLD链接,您可以在页面底部查看whois信息。
有了服务器列表,我们现在可以编写一个执行whois查找的函数。我们需要做的第一件事是整理域名,以清除whois不需要的任何信息(例如http和www),然后找到TLD,该顶级域名将告诉我们我们需要询问的whois服务器。下一步是打开一个连接,以fsockopen()调用端口43上的Whois服务器名称,然后使用该fwrite()函数发送我们想知道的域名。
function whois_query($domain) { //修复域名: $domain = strtolower(trim($domain)); $domain = preg_replace('/^http:\/\//i','',$domain); $domain = preg_replace('/^www\./i', '', $domain); $domain = explode('/', $domain); $domain = trim($domain[0]); //从域名拆分TLD $_domain = explode('.', $domain); $lst = count($_domain)-1; $ext = $_domain[$lst]; //Whois服务器列表 //大部分取自www.iana.org/domains/root/db/ $servers = array( "biz" => "whois.neulevel.biz", "com" => "whois.internic.net", "us" => "whois.nic.us", "coop" => "whois.nic.coop", "info" => "whois.nic.info", "name" => "whois.nic.name", "net" => "whois.internic.net", "gov" => "whois.nic.gov", "edu" => "whois.internic.net", "mil" => "rs.internic.net", "int" => "whois.iana.org", "ac" => "whois.nic.ac", "ae" => "whois.uaenic.ae", "at" => "whois.ripe.net", "au" => "whois.aunic.net", "be" => "whois.dns.be", "bg" => "whois.ripe.net", "br" => "whois.registro.br", "bz" => "whois.belizenic.bz", "ca" => "whois.cira.ca", "cc" => "whois.nic.cc", "ch" => "whois.nic.ch", "cl" => "whois.nic.cl", "cn" => "whois.cnnic.net.cn", "cz" => "whois.nic.cz", "de" => "whois.nic.de", "fr" => "whois.nic.fr", "hu" => "whois.nic.hu", "ie" => "whois.domainregistry.ie", "il" => "whois.isoc.org.il", "in" => "whois.ncst.ernet.in", "ir" => "whois.nic.ir", "mc" => "whois.ripe.net", "to" => "whois.tonic.to", "tv" => "whois.tv", "ru" => "whois.ripn.net", "org" => "whois.pir.org", "aero" => "whois.information.aero", "nl" => "whois.domain-registry.nl", "uk" => "whois.nic.uk", "us" => "whois.nic.us", "travel" => "whois.nic.travel", "gov" => "whois.dotgov.gov", "it" => "whois.nic.it" ); if (!isset($servers[$ext])) { die('Error: No matching whois server found!'); } $nic_server = $servers[$ext]; $output = ''; //连接到whois服务器: if ($conn = fsockopen($nic_server, 43)) { fwrite($conn, $domain."\r\n"); while (!feof($conn)) { $output .= fgets($conn, 128); } fclose($conn); } else { die('Error: Could not connect to ' . $nic_server . '!'); } return $output; }
如果我们无法连接到服务器或找不到TLD服务器,则该程序将退出。请注意,该列表并不完全完整,但足以满足99%讲英语的网站的需求。
要运行该功能,只需给它一个域名并打印输出,如果要保留空白,请使用pre标记。
echo'
'.whois_query('google.com').'';