PHP中根据IP地址判断所在城市等信息
获得IP地址
在 PHP 中得到当前访问者的IP地址,还是比较简单的:
$ip = $_SERVER['REMOTE_ADDR']
将IP转换为城市等信息
淘宝提供了一个IP数据接口: http://ip.taobao.com/service/getIpInfo.php?ip=ip地址
$response = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
$result = json_decode($response);
print_r($result);
输出结果为:
stdClass Object
(
[code] => 0
[data] => stdClass Object
(
[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] => 183.16.191.102
)
)