欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

通过IP获取对应ip的城市信息的函数

程序员文章站 2022-05-23 17:37:29
...
通过IP获取对应ip的城市信息的函数
/**
* 通过IP获取对应城市信息(该功能基于淘宝第三方IP库接口)
* @param $ip IP地址,如果不填写,则为当前客户端IP
* @return 如果成功,则返回数组信息,否则返回false
*/
function getIpInfo($ip){
if(empty($ip)) $ip=get_client_ip(); //get_client_ip()为tp自带函数,如没有,自己百度搜索。此处就不重复复制了
$url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
$result = file_get_contents($url);
$result = json_decode($result,true);
if($result['code']!==0 || !is_array($result['data'])) return false;
return $result['data'];
}

结果例子:
Array
(
[country] => 中国
[country_id] => CN
[area] => 华南
[area_id] => 800000
[region] => 广东省
[region_id] => 440000
[city] => 深圳市
[city_id] => 440300
[county] =>
[county_id] => -1
[isp] => 电信
[isp_id] => 100017
[ip] => 113.116.200.48
)

AD:真正免费,域名+虚机+企业邮箱=0元