freegeoip 不再工作了

freegeoip doesn't work anymore

几个月前,我创建了一个代码来检测访问者国家并显示法定饮酒年龄。 欧盟国家为 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 上,但我无法修复它。

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

我用 http://getcitydetails.geobytes.com/GetCityDetails?callback=?

替换了 http://freegeoip.net/json/

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

现在代码又可以工作了。

你也可以像 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;
    }
}