您可以这样做,从 url 获取主机名,然后使用 gethostbyname() 获取 IP,然后从 whois 站点获取有关 IP 的一些信息。
<?php
$url = 'http://www.washingtontimes.com/news/2012/sep/18/pentagon-stops-training-partnering-afghan-troops-b/';
$host = parse_url($url,PHP_URL_HOST);
$ip = gethostbyname($host);
$info = get_ip_info($ip);
$result = array('host'=>$host, 'ip'=>$ip, 'info'=>$info);
print_r($result);
/*
Array
(
[host] => www.washingtontimes.com
[ip] => 38.118.71.70
[info] => Array
(
[host] => theconservatives.com
[country] => United States
[country_code] => USA
[continent] => North America
[region] => Virginia
[latitude] => 38.9687
[longitude] => -77.3411
[organization] => Cogent Communications
[isp] => Cogent Communications
)
)
*/
echo $result['info']['country']; //United States
function get_ip_info($ip = NULL){
if(empty($ip)) $ip = $_SERVER['REMOTE_ADDR'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.ipaddresslocation.org/ip-address-locator.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,array('ip'=>$ip));
$data = curl_exec($ch);
curl_close($ch);
preg_match_all('/<i>([a-z\s]+)\:<\/i>\s+<b>(.*)<\/b>/im',$data,$matches,PREG_SET_ORDER);
if(count($matches)==0)return false;
$return = array();
$labels = array(
'Hostname' => 'host',
'IP Country' => 'country',
'IP Country Code' => 'country_code',
'IP Continent' => 'continent',
'IP Region' => 'region',
'IP Latitude' => 'latitude',
'IP Longitude' => 'longitude',
'Organization' => 'organization',
'ISP Provider' => 'isp');
foreach($matches as $info){
if(isset($info[2]) && !is_null($labels[$info[1]])){
$return[$labels[$info[1]]]=$info[2];
}
}
return (count($return))?$return:false;
}
?>