高德api开发IP定位定位ip判断当前城市地址
更新时间:2023-03-29 19:50 已阅读: 次 | 作者:骏马网络 来源:潍坊做网站$ip = getClientIp();
$ret = getCity($ip);
print_r($ret);
function getCity($ip){
$url = "https://restapi.amap.com/v3/ip";
$param = [
'ip' => $ip,
'key' => '73905ec84f2dccb7aabbdd525685cb4c'
];
$ret = http($url, $param);
$data = json_decode($ret, true);
return [
'province' => $data['province'],
'city' => $data['city']
];
}
// 获取客户端IP地址
function getClientIp() {
static $ip = NULL;
if ($ip !== NULL) return $ip;
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法验证
$ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
return $ip;
}
文章引用:http://www.wfwz.net/news/258.html如需转载,请注明出处。