一个实现Whois的类
长沙北大青鸟作者:科泰校区匿名
摘要:这个Whois类中只有一个方法,通过这个方法可以对几乎所有的domain name进行查询,并将查询结果返回。但对于新的domain name例如:.tv,.info,.biz等还不支持。代码如下:<?/**************************************
这个Whois类中只有一个方法,通过这个方法可以对几乎所有的domain name进行查询,并将查询结果返回。但对于新的domain name例如:.tv,.info,.biz等还不支持。
代码如下:
<?
/***************************************************
* ?Sloppycode.net All rights reserved.
* This is a standard copyright header for all source code appearing
* at sloppycode.net. This application/class/script may be redistributed,
* as long as the above copyright remains intact.
* Comments to sloppycode@sloppycode.net
***************************************************/
/***************************************************
* @title Whois wrapper for most global TLDs
* @author C.Small
* @version 1.4 - Timeout and whois_server properties added.
* @version 1.3 - Temporary fix for .name,.pro domains
* @version 1.2 - Error catching for .tv domains
* @version 1.1 - Converted to php
* @version 1.0 - Perl version [http://www.sloppycode.net/sloppycode/Perl[CGI]/s29.html]
***************************************************/
Class Whois
{
var $whois_server;
var $timeout = 30;
function lookup($domain)
{
$result = "";
$parts = array();
$host = "";
// .tv don't allow access to their whois
if (strstr($domain,".tv"))
{
$result = "'.tv' domain names require you to have an account to do whois searches.";
// New domains fix (half work, half don't)
} elseif (strstr($domain,".name") || strstr($domain,".pro") >0){
$result = ".name,.pro require you to have an account to do whois searches.";
} else{
if (empty($this->whois_server))
{
$parts = explode(".",$domain);
$testhost = $parts[sizeof($parts)-1];
$whoisserver = $testhost . ".whois-servers.net";
$this->host = gethostbyname($whoisserver);
$this->host = gethostbyaddr($this->host);
if ($this->host == $testhost)
{
$this->host = "whois.internic.net";
}
flush();
}
$whoisSocket = fsockopen($this->host,43, $errno, $errstr, $this->timeout);
if ($whoisSocket)
{
fputs($whoisSocket, $domain."1512");
while (!feof($whoisSocket))
{
$result .= fgets($whoisSocket,128) . "<br>";
}
fclose($whoisSocket);
}
}
return $result;
}
}
?>
上面的代码中定义了一个方法:lookup($domain),下面的这段代码就是测试这个类的功能的。
<?
include("clsWhois.php");
$whois = new Whois();
echo $whois->lookup("code-labs.com");
?>