腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress
返回值 var IPData = new Array("61.135.152.194","","北京市","");
新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42
返回值 var remote_ip_info = {"ret":1,"start":"218.192.0.0","end":"218.192.7.255","country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u5e7f\u5dde","district":"","isp":"\u6559\u80b2\u7f51","type":"\u5b66\u6821","desc":"\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662"};
使用腾迅的api接口,php获取ip地址以及所在城市
http://fw.qq.com/ipaddress返回类似:var IPData = new Array("61.51.71.183","","北京市","");
代码
public static function positionAction($ip) {
$ch = curl_init("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}");
//curl_setopt($ch,CURLOPT_ENCODING ,'utf8');
curl_setopt($ch, CURLOPT_TIMEOUT, );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
$location = json_decode(curl_exec($ch));
curl_close($ch);
if ($location->ret == -) {
return [
'status' => ,
'info' => '获取地理位置失败'
];
}
return [
'status' => ,
'info' => '获取地理位置成功',
'handle' => $location
];
}
php用淘宝接口获取ip的城市省份
/**
* 通过淘宝IP接口获取IP地理位置
* @param string $ip
* @return: string
**/
function getCity($ip)
{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ipinfo=json_decode(file_get_contents($url));
if($ipinfo->code=='1'){
return false;
}
$city = $ipinfo->data->region.$ipinfo->data->city;
return $city;
}
header("Content-Type:text/html;charset=utf-8");
// 这样调用,显示山东省临沂市
var_dump(getCity("112.234.69.189"));
?>