zoukankan      html  css  js  c++  java
  • php ip 城市(百度地图)

    注:前往百度地图api开放平台登录

      在控制台创建一个应用

      拿到访问应用的AK

    代码:

    function getClientIP()
        {
            $realip = '';
            $unknown = 'unknown';
            if (isset($_SERVER)){
            if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            foreach($arr as $ip){
            $ip = trim($ip);
            if ($ip != 'unknown'){
            $realip = $ip;
            break;
            }
            }
            }else if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)){
            $realip = $_SERVER['HTTP_CLIENT_IP'];
            }else if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){
            $realip = $_SERVER['REMOTE_ADDR'];
            }else{
            $realip = $unknown;
            }
            }else{
            if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)){
            $realip = getenv("HTTP_X_FORWARDED_FOR");
            }else if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)){
            $realip = getenv("HTTP_CLIENT_IP");
            }else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)){
            $realip = getenv("REMOTE_ADDR");
            }else{
            $realip = $unknown;
            }
            }
            $realip = preg_match("/[d.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
            return $realip;
        }
        //获取所在城市
        public function getCity()
        {
            // 获取当前位置所在城市
            $getIp = $this->getClientIP();
         // url中的ak参数换成 百度地图api开放平台中拿到的AK $content
    = file_get_contents("http://api.map.baidu.com/location/ip?ak={$ak}&ip={$getIp}&coor=bd09ll"); $json = json_decode($content); $address = $json->{'content'}->{'address'};//按层级关系提取address数据 $data['address'] = $address; $return['province'] = mb_substr($data['address'],0,3,'utf-8'); $return['city'] = mb_substr($data['address'],3,3,'utf-8'); return $return; }
  • 相关阅读:
    web前端开发面试被虐篇(一)
    JSON字符串 拼接与解析
    IntelliJ IDEA 2017 Dubbo Elastic-job Redis Zookeeper RabbitMQ FastDFS MyCat 简介以及部分实现(三)
    IntelliJ IDEA 2017 MySQL5 绿色版 Spring 4 Mybatis 3 配置步骤详解(二)
    IntelliJ IDEA 2017 JDK Tomcat Maven 配置步骤详解(一)
    简单练习
    C#实现上传/下载Excel文档
    javascript练习题
    转 浏览器滚动条以及常用样式汇总
    深入基础(五)工具模块,常用模块(2)
  • 原文地址:https://www.cnblogs.com/LF-place/p/13545937.html
Copyright © 2011-2022 走看看