function getAddressFromIp($ip)
{
$urlTaobao = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
$urlSina = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $ip;
$json = file_get_contents($urlTaobao);
$jsonDecode = json_decode($json);
//如果取不到就去取新浪的
if($jsonDecode->code == 0)
{
$data['country'] = $jsonDecode->data->country;
$data['province'] = $jsonDecode->data->region;
$data['city'] = $jsonDecode->data->city;
$data['isp'] = $jsonDecode->data->isp;
return $data;
}else
{
$json = file_get_contents($urlSina);
$jsonDecode = json_decode($json);
$data['country'] = $jsonDecode->country;
$data['province'] = $jsonDecode->province;
$data['city'] = $jsonDecode->city;
$data['isp'] = $jsonDecode->isp;
$data['district'] = $jsonDecode->district;
return $data;
}
}
// 根据ip获取城市名
function getCityFromIp($ip)
{
$data = getAddressFromIp($ip);
return $data['city'];
}
$ip = “111.13.100.92”;
运行结果:
Array
(
[country] => 中国
[province] => 北京
[city] => 北京
[isp] => 移动
)