【问题标题】:freegeoip doesn't work anymorefreegeoip 不再工作了
【发布时间】:2018-06-19 18:36:57
【问题描述】:

几个月前,我创建了一个代码,用于检测访问国家/地区并显示法定饮酒年龄。 欧盟国家为 18,其他国家为 21。

我正在使用 freegeoip。

代码运行良好,但现在我发现它不再运行了。

    $.get("http://freegeoip.net/json/", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#country_code").html(response.country_code);
    if(response.country_code=='AL','AD','AT','BY','BE','BA','BG','HR','CY','CZ','DK','EE','FO','FI','FR','DE','GI','GR','HU','IS','IE','IT','LV','LI','LT','LU','MK','MT','MD','MC','NL','NO','PL','PT','RO','RU','SM','RS','SK','SI','ES','SE','CH','UA','VA','RS','IM','RS','ME') {
        $(".age").html("18");
    } else {
        $(".age").html("21");
    }
}, "jsonp");

这里显示年龄:

<span>ARE YOU</span> OVER <span class="age"></span>?

我认为问题出在 freegeoip 但我无法解决。

【问题讨论】:

  • 我投票结束这个问题,因为它是关于第三方服务的可用性。

标签: javascript jquery json freegeoip


【解决方案1】:

你也可以像 freegeoip 一样使用 ip-api.com :

function getDataOfIp($ip) {
    try {
        $pageContent = file_get_contents('http://ip-api.com/json/' . $ip);
        $parsedJson  = json_decode($pageContent);
        return [
            "country_name" => $parsedJson->country,
            "country_code" => $parsedJson->countryCode,
            "time_zone" => $parsedJson->timezone
        ];
    } 
    catch (Exception $e) {
        return null;
    }
}

【讨论】:

  • 工作完美 我在输出中添加了城市和州 非常感谢我不得不疯狂地挖掘以找到一个无需使用 JavaScript 即可提供正确城市的网站。
【解决方案2】:

我刚刚找到了问题的答案。 我注意到 freegeoip.net 网站无法正常工作,所以我更改了服务。

我将http://freegeoip.net/json/ 替换为http://getcitydetails.geobytes.com/GetCityDetails?callback=?

并添加了&lt;script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&amp;variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet,GeobytesFqcn"&gt;&lt;/script&gt;

现在代码又可以工作了。

【讨论】:

  • 我认为现在您必须使用参数回调才能使其工作:freegeoip.net/json/?callback=foobar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
相关资源
最近更新 更多