IPGeolocation将是您更好的选择。他们建立自己的数据库并每周更新两次数据库。他们在国家一级有 99% 的准确率,在城市一级有 75% 的准确率。他们还提供付费和免费(每天 1000 个请求)API 计划,您可以在此处查看:https ://ipgeolocation.io/pricing.html 。
为了方便开发人员,IPGeolocation API 提供了一些适用于各种编程语言的 SDK:
获取 IP 地址地理位置信息的端点:
此端点旨在从服务器端调用。
$ curl 'https://api.ipgeolocation.io/ipgeo?apiKey=API_KEY&ip=8.8.8.8'
JSON 响应将是:
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"continent_code": "NA",
"continent_name": "North America",
"country_code2": "US",
"country_code3": "USA",
"country_name": "United States",
"country_capital": "Washington",
"state_prov": "California",
"district": "",
"city": "Mountain View",
"zipcode": "94043",
"latitude": "37.4229",
"longitude": "-122.085",
"is_eu": false,
"calling_code": "+1",
"country_tld": ".us",
"languages": "en-US,es-US,haw,fr",
"country_flag": "https://ipgeolocation.io/static/flags/us_64.png",
"isp": "Level 3 Communications",
"connection_type": "",
"organization": "Google Inc.",
"geoname_id": "5375480",
"currency": {
"code": "USD",
"name": "US Dollar",
"symbol": "$"
},
"time_zone": {
"name": "America/Los_Angeles",
"offset": -8,
"current_time": "2019-01-14 03:30:00.135-0800",
"current_time_unix": 1547465400.135,
"is_dst": false,
"dst_savings": 1
}
}
您还可以从整个响应中过滤特定参数。
获取访问者的位置后,您还可以使用以下简单代码根据他的语言/货币将他重定向到页面:
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (4 === this.readyState && 200 === this.status) {
var json = JSON.parse(this.responseText);
if (json.country_code2 == "FR" && window.location.hostname !== "http://fr.exmaple.com") {
window.location.href = "https://fr.exmaple.com";
} else if (json.country_code2 == "US" && window.location.hostname !== "http://us.exmaple.com") {
window.location.href = "https://us.exmaple.com";
} else if (json.country_code2 == "UK" && window.location.hostname !== "http://uk.exmaple.com") {
window.location.href = "https://uk.exmaple.com";
}else {
window.location.href = "https://exmaple.com";
}
}
}
request.open("GET", "https ://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY&fields=country_code2", false);
request.setRequestHeader("Accept", "application/json");
request.send();
我希望这将是您的最佳选择。