使用PHP+淘宝IP地址库接口获得IP所属地理位置

2015年4月8日16:57:33 评论 5,500

有时候需要判断IP地址所属地理位置。下面的实例是通过淘宝IP地址库API接口获取IP地址属地。很简单,看代码:

<?php
/**

* 通过淘宝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;
}

// for example
print_r(getCity("125.77.254.162"));
福建省厦门市
?>
weinxin
聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: