58

案例:php利用淘宝IP库获取用户ip地理位置

 5 years ago
source link: https://www.linuxprobe.com/php-ip-address.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我们查ip的时候都是利用ip138查询的,不过那个有时候是不准确的,还不如自己引用淘宝的ip库来查询,这样准确度还高一些。不多说了,介绍一下:

淘宝IP地址库,淘宝公布了他们的IP库http://ip.taobao.com/,还有REST API接口,不过每个用户的访问频率需小于10qps,访问方 式:http://ip.taobao.com/service/getIpInfo.PHP?ip=[ip地址字串],返回内容以json格式的。具有IP查询,IP统计等功能。各大运营商拥有的IP数等信息。接下来介绍一下获取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"));
?>

调用的时候吧固定的ip替换成你想查询的ip就可以了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK