PHP获取指定IP地址的whois数据
PHP #ip地址 #whois2012-10-30 13:08
要获取指定IP地址的whois数据,可使用如下代码:
<?php
/**
Program to perform ip whois
Silver Moon
m00n.silv3r@gmail.com
*/
$ip = "74.65.112.23";
$whois = get_whois($ip);
echo "<pre>$whois</pre>";
/**
Get the whois content of an ip by selecting the correct server
*/
function get_whois($ip)
{
$w = get_whois_from_server('whois.iana.org' , $ip);
preg_match('@whois.[w.]*@si' , $w , $data);
$whois_server = $data[0];
//echo $whois_server;
//now get actual whois data
$whois_data = get_whois_from_server($whois_server , $ip);
return $whois_data;
}
/**
Get the whois result from a whois server
return text
*/
function get_whois_from_server($server , $ip)
{
$data = '';
#Before connecting lets check whether server alive or not
#Create the socket and connect
$f = fsockopen($server, 43, $errno, $errstr, 3); //Open a new connection
if(!$f)
{
return '';
}
#Set the timeout limit for read
if(!stream_set_timeout($f , 3))
{
die('Unable to set set_timeout'); #Did this solve the problem ?
}
#Send the IP to the whois server
if($f)
{
fputs($f, "$ip
");
}
/*
Theory : stream_set_timeout must be set after a write and before a read for it to take effect on the read operation
If it is set before the write then it will have no effect : http://in.php.net/stream_set_timeout
*/
//Set the timeout limit for read
if(!stream_set_timeout($f , 3))
{
die('Unable to stream_set_timeout'); #Did this solve the problem ?
}
//Set socket in non-blocking mode
stream_set_blocking ($f, 0 );
//If connection still valid
if($f)
{
while (!feof($f))
{
$data .= fread($f , 128);
}
}
//Now return the data
return $data;
}相关文章
- php5中北京时间差8小时的解决办法 2012/10/30
- PHP计算两个时间相差的天/时/分/秒 2012/10/30
- PHP文件夹复制的函数 2012/10/30
- PHP生成缩略图的函数 2012/10/30
- php获取文件类型和文件信息 2012/10/30
- PHP将多维数组转为一维数组的函数 2012/10/30
- PHP实现的session数据库交互类 2012/10/30
- PHP获取客户端IP地址的函数 2012/10/30
- php多文件上传的三种方法 2012/10/29
- PHP支持多种编码截取中文字符串的函数 2012/10/29