【问题标题】:Geolocation sometimes does not work [closed]地理位置有时不起作用[关闭]
【发布时间】:2015-01-07 09:42:26
【问题描述】:

我在我的网站上使用地理位置脚本来创建一个国家和城市位置,每当用户访问我的页面时,它就会在一个句子中显示。 但是,该功能无法按预期工作,有时会给出错误的国家和城市。

这是地理位置脚本的代码: <script>jQuery.ajax({url:"//freegeoip.net/json/",type:"POST",dataType:"jsonp",success:function(b){jQuery("#findcity").html(b.city);jQuery("#region-code").html(b.region_code);jQuery("#region-name").html(b.region_name);jQuery("#areacode").html(b.areacode);jQuery("#ip").html(b.ip);jQuery("#zipcode").html(b.zipcode);jQuery("#longitude").html(b.longitude);jQuery("#latitude").html(b.latitude);jQuery("#findcountry").html(b.country_name);jQuery("#country-code").html(b.country_code)}});</script>

下一行代码是在body标签的一句话中高亮国家和城市 <h4 itemprop=headline align=center>Hello my <span id=findcountry></span> friend, how is <span id=findcity></span> today have got an awesome offer for you.</h4>

目前 freegeoip.net 是调用国家和城市数据的地方,但它们目前经常关闭并因此导致句子出现拼写错误。

有没有更好的解决方案?

【问题讨论】:

    标签: php geoip freegeoip


    【解决方案1】:

    试试这个代码:

    注意:从 HERE 创建您的 api 密钥

            $remoteIp = $_SERVER['HTTP_HOST'];
            $geoLocationUrl="http://api.db-ip.com/addrinfo?   addr=$remoteIp&api_key='YOUR API KEY'";
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $geoLocationUrl);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
            $auth = curl_exec($curl);
            if($auth)
            {
            $json = json_decode($auth,true); 
            echo $address =  $json['address'];
             echo $country =$json['country'];
             echo $state =$json['stateprov'];
             echo $city =$json['city'];
            }
            else{
            echo 'ERRROR';
    
            }
    

    【讨论】:

    • @user2366920 :如果您对我的代码有任何疑问,请告诉我
    • @user2366920 你试过我的答案了吗?
    • 请不要回答离题的问题。
    • @cybermonkey 题外话怎么样?在获取代码方面寻求帮助?
    • @user2366920 就是这样。您应该查看How to Ask 了解更多信息。
    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    相关资源
    最近更新 更多