实例
$ip = 'unknown'; $unknown = 'unknown'; if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) { // 使用透明代理、欺骗性代理的情况 $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) { // 没有代理、使用普通匿名代理和高匿代理的情况 $ip = $_SERVER['REMOTE_ADDR']; } // 处理多层代理的情况 if (strpos($ip, ',') !== false) { // 输出第一个IP $ip = reset(explode(',', $ip)); } $content = file_get_contents("http://api.map.baidu.com/location/ip?ak=2TGbi6zzFm5rjYKqPPomh9GBwcgLW5sS&ip={$ip}&coor=bd09ll"); $json = json_decode($content); $address = $json->content->address_detail; $return['province'] = $address->province; $return['city'] = $address->city; pr($return); pr($json);
运行实例 »
点击 "运行实例" 按钮查看在线实例