PHP版域名whois信息(过期时间)查询工具

2014-07-24 09:10

2011-05-21更新:

很多whois服务器已经停止工作了;

很多whois协议格式是不一样的;

因为要写域名过期自动提醒工具,重新更新了代码在附件里,含提取过期时间的正则。目前支持22种后缀的域名。

=============================

修改自网上流传的版本,并作了修正:

增加了两百多种域名的支持;

修正了过期时间解析错误的BUG,改进了效率;

 

预期推出域名过期自动提醒服务。不过也要看心情和动力的。

: )

PHP代码
  1. <?php   
  2. if(!isset($_GET['domain'])){   
  3.     echo "1.您未指定要查询的域名!<br />";   
  4.     echo "2.接口使用方法:/whois.php?domain=<FONT color=#ff0000>域名</FONT><br />";   
  5.     echo "3.<FONT color=#ff0000>域名</FONT>请勿加<FONT color=#ff0000>www.</FONT><br />";   
  6.     exit;   
  7. }   
  8. $domain  = $_GET['domain'];   
  9. class SearchDomain   
  10. {   
  11.     private $arr_whois_servers = array(   
  12.         'com'=>'whois.internic.net',   
  13.         'net'=>'whois.internic.net',   
  14.         'org'=>'whois.pir.org',   
  15.         'asia'=>'whois.crsnic.net',   
  16.         'ac'=>'whois.nic.uk',   
  17.         'ad'=>'whois.nic.ad',   
  18.         'ae'=>'whois.uaenic.ae',   
  19.         'ag'=>'whois.nic.ag',   
  20.         'ai'=>'whois.offshore.com.ai',   
  21.         'al'=>'whois.inima.al',   
  22.         'an'=>'whois.una.net',   
  23.         'ao'=>'whois.fccn.pt',   
  24.         'aq'=>'whois.nsrc.org',   
  25.         'ar'=>'whois.nic.ar',   
  26.         'as'=>'whois.nic.as',   
  27.         'am'=>'whois.amnic.net',   
  28.         'au'=>'whois.amnic.net',   
  29.         'at'=>'whois.amnic.net',   
  30.         'az'=>'whois.domain.az',   
  31.         'ba'=>'whois.utic.net.ba',   
  32.         'bb'=>'whois.domains.org.bb',   
  33.         'be'=>'whois.dns.be',   
  34.         'bf'=>'whois.onatel.bf',   
  35.         'bg'=>'whois.digsys.bg',   
  36.         'bh'=>'whois.batelco.com.bh',   
  37.         'bi'=>'whois.nic.bi',   
  38.         'biz'=>'whois.nic.biz',   
  39.         'bm'=>'whois.bermudanic.bm',   
  40.         'bn'=>'whois.brunet.bn',   
  41.         'bo'=>'whois.digsys.bo',   
  42.         'by'=>'whois.unibel.by',   
  43.         'bz'=>'whois.belizenic.bz',   
  44.         'br'=>'whois.registro.fapesp.br',   
  45.         'bt'=>'whois.nic.bt',   
  46.         'bv'=>'whois.norid.no',   
  47.         'ca'=>'whois.cira.ca',   
  48.         'cc'=>'whois.nic.cc',   
  49.         'cd'=>'whois.nic.cd',   
  50.         'cf'=>'whois.socatel.intnet.cf',   
  51.         'cg'=>'whois.nic.cg',   
  52.         'ch'=>'whois.nic.ch',   
  53.         'ci'=>'whois.aipdi.ci',   
  54.         'ck'=>'whois.oyster.net.ck',   
  55.         'cl'=>'whois.nic.cl',   
  56.         'cm'=>'whois.info.intelcam.cm',   
  57.         'cn'=>'whois.cnnic.net.cn',   
  58.         'co'=>'whois.nic.uniandes.edu.co',   
  59.         'coop'=>'whois.nic.coop',   
  60.         'cu'=>'whois.nic.cu',   
  61.         'cx'=>'whois.nic.cx',   
  62.         'cy'=>'whois.nic.cy',   
  63.         'cz'=>'whois.nic.cz',   
  64.         'de'=>'whois.denic.de',   
  65.         'dk'=>'whois.dk-hostmaster.dk',   
  66.         'dj'=>'whois.intnet.dj',   
  67.         'do'=>'whois.nic.do',   
  68.         'dz'=>'whois.nic.dz',   
  69.         'ec'=>'whois.nic.ec',   
  70.         'edu'=>'whois.educause.net',   
  71.         'ee'=>'whois.eenet.ee',   
  72.         'eg'=>'whois.frcu.eun.eg',   
  73.         'es'=>'whois.nic.es',   
  74.         'et'=>'whois.telecom.net.et',   
  75.         'fi'=>'whois.ripe.net',   
  76.         'fj'=>'whois.usp.ac.fj',   
  77.         'fk'=>'whois.fidc.org.fk',   
  78.         'fm'=>'whois.dot.fm',   
  79.         'fo'=>'whois.nic.fo',   
  80.         'fr'=>'whois.nic.fr',   
  81.         'gb'=>'whois.nic.uk',   
  82.         'ge'=>'whois.nic.net.ge',   
  83.         'gf'=>'whois.nplus.gf',   
  84.         'gg'=>'whois.nic.gg',   
  85.         'gh'=>'whois.ghana.com',   
  86.         'gi'=>'whois.gibnet.gi',   
  87.         'gl'=>'whois.nic.gl',   
  88.         'gm'=>'whois.nic.gm',   
  89.         'gn'=>'whois.psg.com',   
  90.         'gov'=>'whois.nic.gov',   
  91.         'gp'=>'whois.nic.gp',   
  92.         'gq'=>'whois.getesa.gq',   
  93.         'gr'=>'whois.hostmaster.gr',   
  94.         'gs'=>'whois.adamsnames.tc',   
  95.         'gt'=>'whois.uvg.edu.gt',   
  96.         'gu'=>'whois.gadao.gov.gu',   
  97.         'hk'=>'whois.hkdnr.net.hk',   
  98.         'hm'=>'whois.registry.hm',   
  99.         'hn'=>'whois.nic.hn',   
  100.         'hr'=>'whois.carnet.hr',   
  101.         'hu'=>'whois.nic.hu',   
  102.         'id'=>'whois.idnet.net.id',   
  103.         'ie'=>'whois.domainregistry.ie',   
  104.         'il'=>'whois.isoc.org.il',   
  105.         'im'=>'whois.nic.im',   
  106.         'in'=>'whois.domain.ncst.ernet.in',   
  107.         'info'=>'whois.afilias.com',   
  108.         'int'=>'whois.iana.org',   
  109.         'io'=>'whois.nic.io',   
  110.         'ir'=>'whois.nic.ir',   
  111.         'is'=>'whois.isnic.is',   
  112.         'it'=>'whois.nic.it',   
  113.         'je'=>'whois.isles.net',   
  114.         'jo'=>'whois.nic.net.jo',   
  115.         'jp'=>'whois.nic.ad.jp',   
  116.         'ke'=>'whois.nbnet.co.ke',   
  117.         'kg'=>'whois.domain.kg',   
  118.         'kh'=>'whois.mptc.gov.kh',   
  119.         'kn'=>'whois.clu.edu',   
  120.         'kr'=>'whois.nic.or.kr',   
  121.         'kw'=>'whois.domainname.net.kw',   
  122.         'kz'=>'whois.domain.kz',   
  123.         'lb'=>'whois.aub.edu.lb',   
  124.         'lc'=>'whois.isisworld.lc',   
  125.         'li'=>'whois.nic.li',   
  126.         'lk'=>'whois.nic.lk',   
  127.         'lr'=>'whois.psg.com',   
  128.         'ls'=>'whois.co.ls',   
  129.         'lt'=>'whois.domreg.lt',   
  130.         'lu'=>'whois.dns.lu',   
  131.         'lv'=>'whois.nic.lv',   
  132.         'ly'=>'whois.nic.ly',   
  133.         'ma'=>'whois.iam.ma',   
  134.         'mc'=>'whois.nic.mc',   
  135.         'md'=>'whois.ripe.net',   
  136.         'mil'=>'whois.nic.mil',   
  137.         'mg'=>'whois.orstom.mg',   
  138.         'mh'=>'whois.nic.net.mh',   
  139.         'mk'=>'whois.mpt.com.mk',   
  140.         'mm'=>'whois.nic.mm',   
  141.         'mn'=>'whois.nic.mn',   
  142.         'mo'=>'whois.umac.mo',   
  143.         'mp'=>'whois.marketplace.mp',   
  144.         'mq'=>'whois.nic.mq',   
  145.         'mr'=>'whois.univ-mkc.mr',   
  146.         'ms'=>'whois.adamsnames.tc',   
  147.         'mt'=>'whois.um.edu.mt',   
  148.         'mu'=>'whois.posix.co.za',   
  149.         'mw'=>'whois.tarsus.net',   
  150.         'mx'=>'whois.nic.mx',   
  151.         'my'=>'whois.mynic.net.my',   
  152.         'mz'=>'whois.nambu.uem.mz',   
  153.         'na'=>'na-nic.com.na',   
  154.         'name'=>'whois.nic.name',   
  155.         'nc'=>'whois.norstom.nc',   
  156.         'nf'=>'whois.names.nf',   
  157.         'ni'=>'whois.nic.ni',   
  158.         'nl'=>'whois.domain-registry.nl',   
  159.         'no'=>'whois.norid.no',   
  160.         'np'=>'whois.mos.com.np',   
  161.         'nu'=>'whois.nic.nu',   
  162.         'nz'=>'whois.domainz.net.nz',   
  163.         'om'=>'whois.gto.net.om',   
  164.         'pa'=>'whois.nic.pa',   
  165.         'pe'=>'whois.ekeko.rcp.net.pe',   
  166.         'ph'=>'whois.domreg.org.ph',   
  167.         'pg'=>'whois.unitech.ac.pg',   
  168.         'pk'=>'whois.pknic.net.pk',   
  169.         'pl'=>'whois.nask.pl',   
  170.         'pm'=>'whois.nic.pm',   
  171.         'pn'=>'whois.nic.pn',   
  172.         'pr'=>'whois.uprr.pr',   
  173.         'ps'=>'whois.nic.ps',   
  174.         'pt'=>'whois.fccn.pt',   
  175.         'py'=>'whois.nic.py',   
  176.         'qa'=>'whois.qatar.net.qa',   
  177.         're'=>'whois.nic.fr',   
  178.         'ro'=>'whois.rnc.ro',   
  179.         'ru'=>'whois.ripn.net',   
  180.         'rw'=>'whois.nic.rw',   
  181.         'sa'=>'whois.saudinic.net.sa',   
  182.         'sb'=>'whois.sbnic.net.sb',   
  183.         'sd'=>'whois.sudatel.sd',   
  184.         'se'=>'whois.nic-se.se',   
  185.         'sg'=>'whois.nic.net.sg',   
  186.         'sh'=>'whois.nic.sh',   
  187.         'si'=>'whois.arnes.si',   
  188.         'sj'=>'whois.norid.no',   
  189.         'sk'=>'whois.sk-nic.sk',   
  190.         'sm'=>'whois.intelcom.sm',   
  191.         'sn'=>'whois.nic.sn',   
  192.         'so'=>'whois.nic.so',   
  193.         'st'=>'whois.st-registry.tecnisys.net',   
  194.         'su'=>'whois.ripn.net',   
  195.         'sv'=>'whois.svnet.org.sv',   
  196.         'sz'=>'whois.iafrica.sz',   
  197.         'tc'=>'whois.adamsnames.tc',   
  198.         'td'=>'whois.tit.td',   
  199.         'tf'=>'whois.adamsnames.tc',   
  200.         'tg'=>'whois.nic.tg',   
  201.         'th'=>'whois.thnic.net',   
  202.         'tj'=>'whois.nic.tj',   
  203.         'tm'=>'whois.nic.tm',   
  204.         'tn'=>'whois.ati.tn',   
  205.         'to'=>'whois.tonic.to',   
  206.         'tp'=>'whois.nic.tp',   
  207.         'tr'=>'whois.metu.edu.tr',   
  208.         'tt'=>'whois.nic.tt',   
  209.         'tw'=>'whois.twnic.net',   
  210.         'tz'=>'whois.psg.com',   
  211.         'tv'=>'whois.networksolutions.com',   
  212.         'ua'=>'nic.net.ua',   
  213.         'ug'=>'whois.nic.ug',   
  214.         'uk'=>'whois.nic.uk',   
  215.         'uk'=>'whois.ripe.net',   
  216.         'um'=>'whois.isi.edu',   
  217.         'us'=>'whois.nic.us',   
  218.         'uy'=>'whois.rau.edu.uy',   
  219.         'uz'=>'whois.freenet.uz',   
  220.         've'=>'whois.nic.ve',   
  221.         'wf'=>'whois.nic.fr',   
  222.         'vg'=>'whois.adamsnames.tc',   
  223.         'vi'=>'whois.nic.vi',   
  224.         'vn'=>'whois.batin.com.vn',   
  225.         'vu'=>'whois.vunic.vu',   
  226.         'ws'=>'samoanic.ws',   
  227.         'yt'=>'whois.nic.fr',   
  228.         'yu'=>'whois.nic.yu',   
  229.         'za'=>'whois.co.za',   
  230.         'zm'=>'whois.zamnet.zm',   
  231.   
  232.     );   
  233.     public $domain="";   
  234.     function SetDomain($udomain)   
  235.     {   
  236.         $this->domain = $udomain;   
  237.     }   
  238.   
  239.     function GetInfo()   
  240.     {   
  241.         $wl = "";   
  242.         $w_server = $this->GetServer();   
  243.         if($w_server==""return "";   
  244.             $fp = fsockopen($w_server, 43, $errno$errstr, 30);   
  245.         if(!$fp) {   
  246.             echo $errstr;   
  247.             return "";   
  248.         }   
  249.         $out = $this->domain."";   
  250.         fputs($fp$out);   
  251.         while (!feof($fp))   
  252.         {   
  253.             $wl = fgets($fp, 255);   
  254.             if(preg_match("/no match/",$wl))   
  255.             {   
  256.                 fclose($fp);   
  257.                 return "ok";   
  258.             }   
  259.             if(preg_match("/Expiration Date/",$wl))   
  260.             {   
  261.                 $lines = explode(":",$wl);   
  262.                 $t = trim($lines[1]);   
  263.                 $ts = explode(" ",$t);   
  264.                 $t = $ts[0];   
  265.                 if(preg_match("/[^0-9-]/",$t))   
  266.                 {   
  267.             return date("Y-m-d",strtotime($t));   
  268.                 }   
  269.                 fclose($fp);   
  270.         return '';   
  271.             }   
  272.         }   
  273.         fclose($fp);   
  274.         return "";   
  275.     }   
  276.     //   
  277.     //获得域名的整个whois信息   
  278.     //   
  279.     function GetWhois()   
  280.     {   
  281.         $wh = "";   
  282.         $w_server = $this->GetServer();   
  283.         if($w_server==""return "";   
  284.         $fp = fsockopen($w_server, 43, $errno$errstr, 30);   
  285.         if(!$fp)   
  286.         {   
  287.             echo $errstr;   
  288.             return "";   
  289.         }   
  290.         $out = $this->domain."";   
  291.         $out .= "Connection: Close";   
  292.         fputs($fp$out);   
  293.         while (!feof($fp))   
  294.         {   
  295.             $wh .= nl2br(fgets($fp, 255));   
  296.         }   
  297.         fclose($fp);   
  298.         return $wh;   
  299.     }   
  300.     //   
  301.     //输出当前域名的状态信息   
  302.     //   
  303.     function PrintSta()   
  304.     {   
  305.         $rs = $this->GetInfo();   
  306.         if($rs=="ok"echo $this->domain." 未注册!<br/>";   
  307.         else if($rs==""echo "无法查询 ".$this->domain." 状态!<br/>";   
  308.         else echo $this->domain." 已注册,到期时间:$rs<br/>";   
  309.     }   
  310.     //   
  311.     //获得 whois 查询服务器   
  312.     //   
  313.     function GetServer()   
  314.     {   
  315.         $udomain=strtolower(substr(strrchr($this->domain,'.'),1));   
  316.         return array_key_exists($udomain,$this->arr_whois_servers) ? $this->arr_whois_servers[$udomain] : '';   
  317.     }   
  318.     //   
  319.     //英语的月份转为数字   
  320.     //   
  321.     function MonthToNum($m)   
  322.     {   
  323.         echo $m;   
  324.         $m = strtolower($m);   
  325.         for($i=1;$i<=12;$i++)   
  326.         {   
  327.             $tt = mktime(0,0,0,$i+1,0,2005);   
  328.             if($m==strtolower(strftime("%b",$tt)))   
  329.             {   
  330.                 if($i>9) return $i-1;   
  331.                 else return "0".$i-1;   
  332.             }   
  333.         }   
  334.     }   
  335. }   
  336.   
  337. $sd = new SearchDomain();   
  338. $sd->SetDomain("$domain");   
  339. //查询域名是否被注册,等价于 $sd->PrintSta();   
  340. $rs = $sd->GetInfo();   
  341. if($rs=="ok"echo $sd->domain." 未注册!<br/>";   
  342. else if($rs==""echo "无法查询 ".$sd->domain." 状态!<br/>";   
  343. else echo $sd->domain." 已注册,到期时间:$rs<br/>";   
  344. //获得域名的详细whois信息   
  345. //echo $sd->GetWhois();   
  346.   
  347. ?>  

2010/05/06 update:请注意:经进一步使用发现上面的whois server返回的并不是统一格式的信息,并且部分whois server无响应。部分小后缀域名是无法查询的,还需要进一步处理。(举例,.kr返回韩文数据,.fr,.de返回某种协议的格式,而.ru返回了另 一种格式的协议,说明页面是俄文的。)

本文采用Creative Commons协议,复制本文需遵守三点:1、保留署名(链接);2、非商业性使用;3、再次创作的作品必须以相同的许可协议发布。

附件: whois.zip (7.15 K, 下载次数:430)


^