<?php
function getiploc_qq($queryip){
$url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryip;
$ch = curl_init($url);
curl_setopt($ch,curlopt_encoding ,'gb2312');
curl_setopt($ch, curlopt_timeout, 10);
curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回
$result = curl_exec($ch);
$result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码
curl_close($ch);
preg_match("@<span>(.*)</span></p>@iu",$result,$iparray);
$loc = $iparray[1];
return $loc;
}
//使用
echo getiploc_qq("183.37.209.57"); //即可得到ip地址所在的地址位置。
?>
三、php利用新浪ip查询接口获取ip地理位置
复制代码 代码如下:
<?php
function getiploc_sina($queryip){
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryip;
$ch = curl_init($url);
curl_setopt($ch,curlopt_encoding ,'utf8');
curl_setopt($ch, curlopt_timeout, 5);
curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
$loc = "";
if($location===false) return "";
if (empty($location->desc)) {
$loc = $location->province.$location->city.$location->district.$location->isp;
}else{ $loc = $location->desc;
}
return $loc;
}
echo getiploc_sina("183.37.209.57");
?>
您可能感兴趣的文章: