您的位置:首页 > 其它

通过新浪接口根据IP获得来源城市

2018-03-07 15:05 471 查看
function getIpLookup($ip = ''){
if(empty($ip)){
return false;
}
$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
if (empty($res)) {
return false;
}
$jsonMatches = array();
preg_match('#\{.+?\}#', $res, $jsonMatches);
if (!isset($jsonMatches[0])) {
return false;
}
$json = json_decode($jsonMatches[0], true);
if (isset($json['ret']) && $json['ret'] == 1) {
$json['ip'] = $ip;
unset($json['ret']);
} else {
return false;
}

return $json;
}
结果:
Array
(
[start] => -1
[end] => -1
[country] => 中国
[province] => 上海
[city] => 上海
[district] =>
[isp] =>
[type] =>
[desc] =>
[ip] => 101.231.183.230
)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: