php根据IP地址获取地理位置

php中文网
发布: 2016-06-23 13:18:06
原创
1849人浏览过

    <?php    header("content-type: text/html; charset=utf-8");     //获取ip地址的方法    function getip(){        if (isset($_server)) {            if (isset($_server['http_x_forwarded_for'])) {                $realip = $_server['http_x_forwarded_for'];            } elseif (isset($_server['http_client_ip'])) {                $realip = $_server['http_client_ip'];            } else {                $realip = $_server['remote_addr'];            }        } else {            if (getenv("http_x_forwarded_for")) {                $realip = getenv( "http_x_forwarded_for");            } elseif (getenv("http_client_ip")) {                $realip = getenv("http_client_ip");            } else {                $realip = getenv("remote_addr");            }        }        return $realip;    }        echo $ip = getip();//通过php的file_get_contents()方法获取地理位置//新浪接口根据ip查询所在区域信息$res0 = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip");$res0 = json_decode($res0,true);print_r($res0);echo "<br/>";//淘宝接口根据ip查询所在区域信息$res1 = file_get_contents("http://ip.taobao.com/service/getipinfo.php?ip=$ip");$res1 = json_decode($res1,true);print_r($res1);echo "<br/>";//通过php的curl获取地理位置//新浪根据ip获取地理位置api    $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip';     $ch = curl_init($url);     curl_setopt($ch,curlopt_encoding ,'utf8');     curl_setopt($ch, curlopt_timeout, 10);     curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回     $location = curl_exec($ch);     $location = json_decode($location);     print_r($location);    curl_close($ch);          $loc = "";     if($location===false) return "";     if (empty($location->desc)) {         $loc = $location->province.$location->city.$location->district.$location->isp;     }else{         $loc = $location->desc;     }     echo  $loc; //腾讯根据ip获取地理位置api    $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1=$ip';     $ch = curl_init($url);     curl_setopt($ch,curlopt_encoding ,'gb2312');     curl_setopt($ch, curlopt_timeout, 10);     curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回     $result = curl_exec($ch);     $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码     curl_close($ch);     preg_match("@<span>(.*)</span></p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/1584">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680266797527.png" alt="钉钉 AI 助理">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/1584">钉钉 AI 助理</a>
                            <p>钉钉AI助理汇集了钉钉AI产品能力,帮助企业迈入智能新时代。</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="钉钉 AI 助理">
                                <span>21</span>
                            </div>
                        </div>
                        <a href="/ai/1584" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="钉钉 AI 助理">
                        </a>
                    </div>
                @iU",$result,$ipArray);     $loc = $ipArray[1];     echo $loc; 
登录后复制

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号