[Asm] 纯文本查看 复制代码 function get_city_by_ip()
{
if (! empty($_SERVER["HTTP_CLIENT_IP"])) {
$cip = $_SERVER["HTTP_CLIENT_IP"];
} elseif (! empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} elseif (! empty($_SERVER["REMOTE_ADDR"])) {
$cip = $_SERVER["REMOTE_ADDR"];
} else {
$cip = "";
}
$url = 'https://restapi.amap.com/v3/ip';
$data = array(
'output' => 'json',
'key' => '16199cf2aca1fb54d0db495a3140b8cb',
'ip' => $cip
);
$postdata = http_build_query($data);
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
if (! empty($result)) {
$res = json_decode($result, true);
if (! empty($res)) {
if (empty($res['province'])) {
$res['province'] = '北京市';
}
if (! empty($res['province']) && $res['province'] == "局域网") {
$res['province'] = '北京市';
}
if(count($res['province']) == 0){
$res['province'] = '北京市';
}
if (count($res['city']) == 0) {
$res['city'] = '北京市';
}
} else {
$res['province'] = '北京市';
$res['city'] = '北京市';
}
return $res;
} else {
return array(
"province" => '北京市',
"city" => '北京市'
);
}
} |