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

有时候需要判断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("121.207.247.202"));
福建省福州市
?>
如需转载请注明出处: 使用淘宝IP地址库接口获得IP所属地理位置  http://www.ttlsa.com/html/1791.html

转载于:https://my.oschina.net/766/blog/211172

猜你喜欢

转载自blog.csdn.net/weixin_34080571/article/details/91493066