2011-05-21更新:
很多whois服务器已经停止工作了;
很多whois协议格式是不一样的;
因为要写域名过期自动提醒工具,重新更新了代码在附件里,含提取过期时间的正则。目前支持22种后缀的域名。
=============================
修改自网上流传的版本,并作了修正:
增加了两百多种域名的支持;
修正了过期时间解析错误的BUG,改进了效率;
预期推出域名过期自动提醒服务。不过也要看心情和动力的。
: )
PHP代码
- <?php
- if(!isset($_GET['domain'])){
- echo "1.您未指定要查询的域名!<br />";
- echo "2.接口使用方法:/whois.php?domain=<FONT color=#ff0000>域名</FONT><br />";
- echo "3.<FONT color=#ff0000>域名</FONT>请勿加<FONT color=#ff0000>www.</FONT><br />";
- exit;
- }
- $domain = $_GET['domain'];
- class SearchDomain
- {
- private $arr_whois_servers = array(
- 'com'=>'whois.internic.net',
- 'net'=>'whois.internic.net',
- 'org'=>'whois.pir.org',
- 'asia'=>'whois.crsnic.net',
- 'ac'=>'whois.nic.uk',
- 'ad'=>'whois.nic.ad',
- 'ae'=>'whois.uaenic.ae',
- 'ag'=>'whois.nic.ag',
- 'ai'=>'whois.offshore.com.ai',
- 'al'=>'whois.inima.al',
- 'an'=>'whois.una.net',
- 'ao'=>'whois.fccn.pt',
- 'aq'=>'whois.nsrc.org',
- 'ar'=>'whois.nic.ar',
- 'as'=>'whois.nic.as',
- 'am'=>'whois.amnic.net',
- 'au'=>'whois.amnic.net',
- 'at'=>'whois.amnic.net',
- 'az'=>'whois.domain.az',
- 'ba'=>'whois.utic.net.ba',
- 'bb'=>'whois.domains.org.bb',
- 'be'=>'whois.dns.be',
- 'bf'=>'whois.onatel.bf',
- 'bg'=>'whois.digsys.bg',
- 'bh'=>'whois.batelco.com.bh',
- 'bi'=>'whois.nic.bi',
- 'biz'=>'whois.nic.biz',
- 'bm'=>'whois.bermudanic.bm',
- 'bn'=>'whois.brunet.bn',
- 'bo'=>'whois.digsys.bo',
- 'by'=>'whois.unibel.by',
- 'bz'=>'whois.belizenic.bz',
- 'br'=>'whois.registro.fapesp.br',
- 'bt'=>'whois.nic.bt',
- 'bv'=>'whois.norid.no',
- 'ca'=>'whois.cira.ca',
- 'cc'=>'whois.nic.cc',
- 'cd'=>'whois.nic.cd',
- 'cf'=>'whois.socatel.intnet.cf',
- 'cg'=>'whois.nic.cg',
- 'ch'=>'whois.nic.ch',
- 'ci'=>'whois.aipdi.ci',
- 'ck'=>'whois.oyster.net.ck',
- 'cl'=>'whois.nic.cl',
- 'cm'=>'whois.info.intelcam.cm',
- 'cn'=>'whois.cnnic.net.cn',
- 'co'=>'whois.nic.uniandes.edu.co',
- 'coop'=>'whois.nic.coop',
- 'cu'=>'whois.nic.cu',
- 'cx'=>'whois.nic.cx',
- 'cy'=>'whois.nic.cy',
- 'cz'=>'whois.nic.cz',
- 'de'=>'whois.denic.de',
- 'dk'=>'whois.dk-hostmaster.dk',
- 'dj'=>'whois.intnet.dj',
- 'do'=>'whois.nic.do',
- 'dz'=>'whois.nic.dz',
- 'ec'=>'whois.nic.ec',
- 'edu'=>'whois.educause.net',
- 'ee'=>'whois.eenet.ee',
- 'eg'=>'whois.frcu.eun.eg',
- 'es'=>'whois.nic.es',
- 'et'=>'whois.telecom.net.et',
- 'fi'=>'whois.ripe.net',
- 'fj'=>'whois.usp.ac.fj',
- 'fk'=>'whois.fidc.org.fk',
- 'fm'=>'whois.dot.fm',
- 'fo'=>'whois.nic.fo',
- 'fr'=>'whois.nic.fr',
- 'gb'=>'whois.nic.uk',
- 'ge'=>'whois.nic.net.ge',
- 'gf'=>'whois.nplus.gf',
- 'gg'=>'whois.nic.gg',
- 'gh'=>'whois.ghana.com',
- 'gi'=>'whois.gibnet.gi',
- 'gl'=>'whois.nic.gl',
- 'gm'=>'whois.nic.gm',
- 'gn'=>'whois.psg.com',
- 'gov'=>'whois.nic.gov',
- 'gp'=>'whois.nic.gp',
- 'gq'=>'whois.getesa.gq',
- 'gr'=>'whois.hostmaster.gr',
- 'gs'=>'whois.adamsnames.tc',
- 'gt'=>'whois.uvg.edu.gt',
- 'gu'=>'whois.gadao.gov.gu',
- 'hk'=>'whois.hkdnr.net.hk',
- 'hm'=>'whois.registry.hm',
- 'hn'=>'whois.nic.hn',
- 'hr'=>'whois.carnet.hr',
- 'hu'=>'whois.nic.hu',
- 'id'=>'whois.idnet.net.id',
- 'ie'=>'whois.domainregistry.ie',
- 'il'=>'whois.isoc.org.il',
- 'im'=>'whois.nic.im',
- 'in'=>'whois.domain.ncst.ernet.in',
- 'info'=>'whois.afilias.com',
- 'int'=>'whois.iana.org',
- 'io'=>'whois.nic.io',
- 'ir'=>'whois.nic.ir',
- 'is'=>'whois.isnic.is',
- 'it'=>'whois.nic.it',
- 'je'=>'whois.isles.net',
- 'jo'=>'whois.nic.net.jo',
- 'jp'=>'whois.nic.ad.jp',
- 'ke'=>'whois.nbnet.co.ke',
- 'kg'=>'whois.domain.kg',
- 'kh'=>'whois.mptc.gov.kh',
- 'kn'=>'whois.clu.edu',
- 'kr'=>'whois.nic.or.kr',
- 'kw'=>'whois.domainname.net.kw',
- 'kz'=>'whois.domain.kz',
- 'lb'=>'whois.aub.edu.lb',
- 'lc'=>'whois.isisworld.lc',
- 'li'=>'whois.nic.li',
- 'lk'=>'whois.nic.lk',
- 'lr'=>'whois.psg.com',
- 'ls'=>'whois.co.ls',
- 'lt'=>'whois.domreg.lt',
- 'lu'=>'whois.dns.lu',
- 'lv'=>'whois.nic.lv',
- 'ly'=>'whois.nic.ly',
- 'ma'=>'whois.iam.ma',
- 'mc'=>'whois.nic.mc',
- 'md'=>'whois.ripe.net',
- 'mil'=>'whois.nic.mil',
- 'mg'=>'whois.orstom.mg',
- 'mh'=>'whois.nic.net.mh',
- 'mk'=>'whois.mpt.com.mk',
- 'mm'=>'whois.nic.mm',
- 'mn'=>'whois.nic.mn',
- 'mo'=>'whois.umac.mo',
- 'mp'=>'whois.marketplace.mp',
- 'mq'=>'whois.nic.mq',
- 'mr'=>'whois.univ-mkc.mr',
- 'ms'=>'whois.adamsnames.tc',
- 'mt'=>'whois.um.edu.mt',
- 'mu'=>'whois.posix.co.za',
- 'mw'=>'whois.tarsus.net',
- 'mx'=>'whois.nic.mx',
- 'my'=>'whois.mynic.net.my',
- 'mz'=>'whois.nambu.uem.mz',
- 'na'=>'na-nic.com.na',
- 'name'=>'whois.nic.name',
- 'nc'=>'whois.norstom.nc',
- 'nf'=>'whois.names.nf',
- 'ni'=>'whois.nic.ni',
- 'nl'=>'whois.domain-registry.nl',
- 'no'=>'whois.norid.no',
- 'np'=>'whois.mos.com.np',
- 'nu'=>'whois.nic.nu',
- 'nz'=>'whois.domainz.net.nz',
- 'om'=>'whois.gto.net.om',
- 'pa'=>'whois.nic.pa',
- 'pe'=>'whois.ekeko.rcp.net.pe',
- 'ph'=>'whois.domreg.org.ph',
- 'pg'=>'whois.unitech.ac.pg',
- 'pk'=>'whois.pknic.net.pk',
- 'pl'=>'whois.nask.pl',
- 'pm'=>'whois.nic.pm',
- 'pn'=>'whois.nic.pn',
- 'pr'=>'whois.uprr.pr',
- 'ps'=>'whois.nic.ps',
- 'pt'=>'whois.fccn.pt',
- 'py'=>'whois.nic.py',
- 'qa'=>'whois.qatar.net.qa',
- 're'=>'whois.nic.fr',
- 'ro'=>'whois.rnc.ro',
- 'ru'=>'whois.ripn.net',
- 'rw'=>'whois.nic.rw',
- 'sa'=>'whois.saudinic.net.sa',
- 'sb'=>'whois.sbnic.net.sb',
- 'sd'=>'whois.sudatel.sd',
- 'se'=>'whois.nic-se.se',
- 'sg'=>'whois.nic.net.sg',
- 'sh'=>'whois.nic.sh',
- 'si'=>'whois.arnes.si',
- 'sj'=>'whois.norid.no',
- 'sk'=>'whois.sk-nic.sk',
- 'sm'=>'whois.intelcom.sm',
- 'sn'=>'whois.nic.sn',
- 'so'=>'whois.nic.so',
- 'st'=>'whois.st-registry.tecnisys.net',
- 'su'=>'whois.ripn.net',
- 'sv'=>'whois.svnet.org.sv',
- 'sz'=>'whois.iafrica.sz',
- 'tc'=>'whois.adamsnames.tc',
- 'td'=>'whois.tit.td',
- 'tf'=>'whois.adamsnames.tc',
- 'tg'=>'whois.nic.tg',
- 'th'=>'whois.thnic.net',
- 'tj'=>'whois.nic.tj',
- 'tm'=>'whois.nic.tm',
- 'tn'=>'whois.ati.tn',
- 'to'=>'whois.tonic.to',
- 'tp'=>'whois.nic.tp',
- 'tr'=>'whois.metu.edu.tr',
- 'tt'=>'whois.nic.tt',
- 'tw'=>'whois.twnic.net',
- 'tz'=>'whois.psg.com',
- 'tv'=>'whois.networksolutions.com',
- 'ua'=>'nic.net.ua',
- 'ug'=>'whois.nic.ug',
- 'uk'=>'whois.nic.uk',
- 'uk'=>'whois.ripe.net',
- 'um'=>'whois.isi.edu',
- 'us'=>'whois.nic.us',
- 'uy'=>'whois.rau.edu.uy',
- 'uz'=>'whois.freenet.uz',
- 've'=>'whois.nic.ve',
- 'wf'=>'whois.nic.fr',
- 'vg'=>'whois.adamsnames.tc',
- 'vi'=>'whois.nic.vi',
- 'vn'=>'whois.batin.com.vn',
- 'vu'=>'whois.vunic.vu',
- 'ws'=>'samoanic.ws',
- 'yt'=>'whois.nic.fr',
- 'yu'=>'whois.nic.yu',
- 'za'=>'whois.co.za',
- 'zm'=>'whois.zamnet.zm',
- );
- public $domain="";
- function SetDomain($udomain)
- {
- $this->domain = $udomain;
- }
- function GetInfo()
- {
- $wl = "";
- $w_server = $this->GetServer();
- if($w_server=="") return "";
- $fp = fsockopen($w_server, 43, $errno, $errstr, 30);
- if(!$fp) {
- echo $errstr;
- return "";
- }
- $out = $this->domain."";
- fputs($fp, $out);
- while (!feof($fp))
- {
- $wl = fgets($fp, 255);
- if(preg_match("/no match/",$wl))
- {
- fclose($fp);
- return "ok";
- }
- if(preg_match("/Expiration Date/",$wl))
- {
- $lines = explode(":",$wl);
- $t = trim($lines[1]);
- $ts = explode(" ",$t);
- $t = $ts[0];
- if(preg_match("/[^0-9-]/",$t))
- {
- return date("Y-m-d",strtotime($t));
- }
- fclose($fp);
- return '';
- }
- }
- fclose($fp);
- return "";
- }
- //
- //获得域名的整个whois信息
- //
- function GetWhois()
- {
- $wh = "";
- $w_server = $this->GetServer();
- if($w_server=="") return "";
- $fp = fsockopen($w_server, 43, $errno, $errstr, 30);
- if(!$fp)
- {
- echo $errstr;
- return "";
- }
- $out = $this->domain."";
- $out .= "Connection: Close";
- fputs($fp, $out);
- while (!feof($fp))
- {
- $wh .= nl2br(fgets($fp, 255));
- }
- fclose($fp);
- return $wh;
- }
- //
- //输出当前域名的状态信息
- //
- function PrintSta()
- {
- $rs = $this->GetInfo();
- if($rs=="ok") echo $this->domain." 未注册!<br/>";
- else if($rs=="") echo "无法查询 ".$this->domain." 状态!<br/>";
- else echo $this->domain." 已注册,到期时间:$rs<br/>";
- }
- //
- //获得 whois 查询服务器
- //
- function GetServer()
- {
- $udomain=strtolower(substr(strrchr($this->domain,'.'),1));
- return array_key_exists($udomain,$this->arr_whois_servers) ? $this->arr_whois_servers[$udomain] : '';
- }
- //
- //英语的月份转为数字
- //
- function MonthToNum($m)
- {
- echo $m;
- $m = strtolower($m);
- for($i=1;$i<=12;$i++)
- {
- $tt = mktime(0,0,0,$i+1,0,2005);
- if($m==strtolower(strftime("%b",$tt)))
- {
- if($i>9) return $i-1;
- else return "0".$i-1;
- }
- }
- }
- }
- $sd = new SearchDomain();
- $sd->SetDomain("$domain");
- //查询域名是否被注册,等价于 $sd->PrintSta();
- $rs = $sd->GetInfo();
- if($rs=="ok") echo $sd->domain." 未注册!<br/>";
- else if($rs=="") echo "无法查询 ".$sd->domain." 状态!<br/>";
- else echo $sd->domain." 已注册,到期时间:$rs<br/>";
- //获得域名的详细whois信息
- //echo $sd->GetWhois();
- ?>
2010/05/06 update:请注意:经进一步使用发现上面的whois server返回的并不是统一格式的信息,并且部分whois server无响应。部分小后缀域名是无法查询的,还需要进一步处理。(举例,.kr返回韩文数据,.fr,.de返回某种协议的格式,而.ru返回了另 一种格式的协议,说明页面是俄文的。)