RFC812定义了一个非常简单的Internet信息查询协议——WHOIS协议。其基本内容是,先向服务器的TCP端口43建立一个连接,发送查询关 键字并加上回车换行,然后接收服务器的查询结果。
不懂的以为这玩意儿有多么神奇,其实也就这个样子。开发WHOIS查询工具最重要的就是获取到所有顶级域名有哪些,这些顶级域名对应的WHOIS服务器地址又是啥?搞定了这些,那问题就搞定了一大半。
后来发现了一个叫“宇宙神盾局(IANA)”的玩意儿,这里罗列了全球顶级域名,然后获取到来个遍历,获取到所有的whois服务器。写一个正则表达式,匹配页面上的WHOIS服务器地址:
function curl($url){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl_exec($ch);
$state=curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($state===200){
return $result;
}else{
return false;
}
}
function getWhois($url){
$_info=curl($url);
$_pattens="/WHOIS Server\:\<\/b\>(.*)\<\/p\>/isU";
if($_info){
if(preg_match($_pattens,$_info,$tmp)){
return $tmp[1];
}else{
return false;
}
}else{
return -2;
}
}
有WHOIS服务器地址之后,再获取输入域名的DOMAIN,然后使用对应的域名服务器去查询。
查询WHOIS原理:
在TCP服务端口43(十进制)连接SRI-NIC服务主机;
发送一个命令,以回车和换行()结尾;
接受相应命令的返回信息,一旦输出结束,服务器将关闭连接。
呐,继续看代码:
//$url:需要查询的域名
//$whois:查询域名对应的whois服务器
function selectWhois($url,$whois){
$output="\n";
if ($conn = fsockopen ($whois, 43)) {
fputs($conn, $url."\r\n");
while(!feof($conn)) {
$output .= "
".fgets($conn)."
}
fclose($conn);
} else {
$output="
查询域名".$url."时,连接WHOIS服务器".$whois."失败
}
$output.="\n";
return $output;
}
域名服务器可以参考下面的数组:
$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"
);
到这里,WHOIS数据已经获取到了,至于更以后的数据处理,自己看着自己的需求来,如果有兴趣,可以去看看全球WHOIS查询-WEB视界
网友评论