dd

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

jerry thinkphp 2015年11月19日 收藏
通过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
)
dd