php调用whois接口域名查询

南楼画角 提交于 2020-03-01 17:32:29

由两部分组成,一个index.php文件,一个whois的接口文件;

 1 <html>
 2 <head>
 3 <title>域名到期查询</title>
 4 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
 5 <link href="css/reset.css" rel="stylesheet" type="text/css" />
 6 <link href="css/index.css" rel="stylesheet" type="text/css" />
 7 </head>
 8 <body>
 9 <script>
10 $(document).ready(function(){
11   $(".dj").toggle(function(){
12     $(".info_main").show();
13     $(".dj").text("-")},
14     function(){
15     $(".info_main").hide();
16     $(".dj").text("+")}
17   );
18 });
19 </script>
20 <h1>域名到期查询:</h1>
21 <form class="yuming_form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
22     <ul>
23         <li>
24         <span>输入域名:</span><input class="yuming_txt" type="text" name="yuming" /><input class="yuming_btn" type="submit" value="查询" />
25         </li>
26 </form>
27 <?php
28 include "whois.php";
29 ?>
30     <div class="info_tit">详情:<span class="dj">+</span></div>
31     <div class="info_main">
32         <?php
33             //查询域名是否被注册,等价于 $sd->PrintSta();
34             $rs = $sd->GetInfo();
35             if($rs=="ok") echo $sd->domain." 未注册!<br/>\r\n";
36             else if($rs=="") echo "无法查询 ".$sd->domain." 状态!<br/>\r\n";
37             else echo $sd->domain." 已注册,到期时间:$rs<br/>\r\n";
38             //获得域名的详细whois信息
39             echo $sd->GetWhois();
40         ?>
41     </div>
42 </body>
43 </html>

whois.php

  1 <?php
  2 class SearchDomain{
  3     var $domain="";
  4     function SetDomain($udomain){
  5         $this->domain = $udomain;
  6     }
  7     //
  8     // 获取whois并分析域名状态
  9     // ok 未被注册
 10     // 非空值 过期时间
 11     // 空值 未知
 12     //
 13     function GetInfo(){
 14         /*
 15         $dinfo = trim($this->GetWhois());
 16         if($dinfo=="") return "";
 17         if(eregi("no match",$dinfo)) return "ok";
 18         //return $rs;
 19         */
 20         $wl = "";
 21         $w_server = $this->GetServer();
 22         if($w_server=="") return "";
 23         $fp = fsockopen($w_server, 43, $errno, $errstr, 30);
 24         if(!$fp){
 25             echo $errstr;
 26             return "";
 27         }
 28         
 29         $out = $this->domain."\r\n";
 30         $out .= "Connection: Close\r\n\r\n";
 31         fputs($fp, $out);
 32         
 33         while (!feof($fp)){
 34             $wl = fgets($fp, 255);
 35             
 36             if(eregi("no match",$wl)){
 37             fclose($fp);
 38             return "ok";
 39             }
 40             if(eregi("Expiration Date",$wl)){
 41                 $lines = split(":",$wl);
 42                 $t = trim($lines[1]);
 43                 $ts = split(" ",$t);
 44                 $t = $ts[0];
 45                 if(ereg("[^0-9-]",$t)){
 46                     $ts = split("-",$t);
 47                     $t = $ts[2]."-".$this->MonthToNum($ts[1])."-".$ts[0];
 48                 }
 49                 fclose($fp);
 50                 return $t;
 51             }
 52         }
 53         fclose($fp);
 54         return "";
 55     }
 56     //
 57     //获得域名的整个whois信息
 58     //
 59     function GetWhois(){
 60         $wh = "";
 61         $w_server = $this->GetServer();
 62         if($w_server=="") return "";
 63         $fp = fsockopen($w_server, 43, $errno, $errstr, 30);
 64         
 65         if(!$fp){
 66             echo $errstr;
 67             return "";
 68         }
 69         $out = $this->domain."\r\n";
 70         $out .= "Connection: Close\r\n\r\n";
 71         fputs($fp, $out);
 72         while (!feof($fp)){
 73             $wh .= nl2br(fgets($fp, 255));
 74         }
 75         fclose($fp);
 76         return $wh;
 77     }
 78     //
 79     //输出当前域名的状态信息
 80     //
 81     function PrintSta(){
 82         $rs = $this->GetInfo();
 83         if($rs=="ok") echo "<li>" . "<font>".$this->domain."</font>" . " 未注册!</li>";
 84         else if($rs=="") echo "<li>" . "无法查询 " . "<font>".$this->domain."</font>" . " 状态!</li>";
 85         else echo "<li>" . "<font>" . $this->domain."</font>" . " 已注册,到期时间:<font>$rs</font></li>";
 86     }
 87     //
 88     //获得 whois 查询服务器
 89     //
 90     function GetServer(){
 91         $udomain=substr($this->domain,-3);
 92         switch($udomain){
 93             case "com":
 94             $w_server="whois.internic.net";
 95             break;
 96             case "net":
 97             $w_server="whois.internic.net";
 98             break;
 99             case "org":
100             $w_server="whois.pir.org";
101             break;
102             case "nfo":
103             $w_server="whois.afilias.info";
104             break;
105             case "biz":
106             $w_server="whois.biz";
107             break;
108             case ".cc":
109             $w_server="whois.nic.cc";
110             break;
111             case "edu":
112             $w_server="whois.educause.net";
113             break;
114             case "gov":
115             $w_server="whois.nic.gov";
116             break;
117             case ".cn":
118             $w_server="whois.cnnic.net.cn";
119             break;
120             default:
121             $w_server="";
122         }
123         return $w_server;
124     }
125 //
126 //英语的月份转为数字
127 //
128     function MonthToNum($m){
129         $m = strtolower($m);
130         
131         for($i=1;$i<=12;$i++){
132             $tt = mktime(0,0,0,$i+1,0,2005);
133             if($m==strtolower(strftime("%b",$tt))){
134                 if($i>9) return $i;
135                 else return "0".$i;
136             }
137         }
138     }
139 }
140     //接收域名值
141     if ($_SERVER["REQUEST_METHOD"] == "POST"){
142        $yuming = test_input($_POST["yuming"]);
143     }
144 
145     function test_input($data){
146        $data = trim($data);
147        $data = stripslashes($data);
148        $data = htmlspecialchars($data);
149        return $data;
150     }
151     /* echo $yuming; */
152     $sd = new SearchDomain();
153     $sd->SetDomain($yuming);
154     $sd->PrintSta();
155     //查询域名是否被注册,等价于 $sd->PrintSta();
156     //$rs = $sd->GetInfo();
157     //if($rs=="ok") echo $sd->domain." 未注册!<br/>\r\n";
158     //else if($rs=="") echo "无法查询 ".$sd->domain." 状态!<br/>\r\n";
159     //else echo $sd->domain." 已注册,到期时间:$rs<br/>\r\n";
160     //获得域名的详细whois信息
161     //echo $sd->GetWhois();
162  ?>

注意两点:

1,index.php要include一下whois文件。

2,通过post方式传name = "yuming"这个值来实现查询,index.php传,whois.php接收。

接收方式

 1 //接收域名值
 2     if ($_SERVER["REQUEST_METHOD"] == "POST"){
 3        $yuming = test_input($_POST["yuming"]);
 4     }
 5 
 6     function test_input($data){
 7        $data = trim($data);
 8        $data = stripslashes($data);
 9        $data = htmlspecialchars($data);
10        return $data;
11     }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!