1

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

4

2 回答 2

4

你也可以像 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;
    }
}
于 2018-06-19T10:36:57.970 回答
0

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

http://freegeoip.net/json/http://getcitydetails.geobytes.com/GetCityDetails?callback=?

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

现在代码再次工作。

于 2016-04-06T06:46:05.680 回答