简体   繁体   English

查找网站访问者地理位置的解决方案

[英]Solutions for finding website visitor's geolocation

I am aware that there are many services which provide geolocation tools for working out where users are visiting from.我知道有许多服务提供地理定位工具来确定用户访问的位置。 The website I'm working on is an e-commerce site which runs in multi-territories.我正在开发的网站是一个在多个地区运行的电子商务网站。 When a visitor hits the website it should work out where they are from and show the relevant currency/language for the user.当访问者访问网站时,它应该计算出他们来自哪里并为用户显示相关的货币/语言。

In the past we have used maxmind but I'm wondering if there are better solutions out there.过去我们使用过maxmind,但我想知道是否有更好的解决方案。 I'm calling out to developers with experience in this area to share their knowledge and expertise on what's available and what the pro's and con's are of various solutions.我呼吁在该领域有经验的开发人员分享他们关于可用解决方案以及各种解决方案的优缺点的知识和专业知识。

Additional info: The website in question runs mainly on a combination of Java (Spring) and PHP (Kohana), so libraries for either of those can also be considered.附加信息:有问题的网站主要在 Java (Spring) 和 PHP (Kohana) 的组合上运行,因此也可以考虑其中任何一个的库。 We already have a solution for working out which currency to use provided we can obtain the ISO code of the visitor's country.如果我们可以获得访问者所在国家/地区的 ISO 代码,我们已经有一个解决方案来确定使用哪种货币。


Update更新
Thought I'd leave a rough explanation of the outcome for anyone who visits this question.以为我会为访问此问题的任何人留下对结果的粗略解释。

Geolocation: I've used freegoip for detecting user location as suggested in this thread.地理位置:我已经使用 freegoip 来检测用户位置,如该线程中建议的那样。
Language: When working out which language to use for the user I have used PHP to check the language set in the user's browser: http://www.php.net/manual/en/function.http-negotiate-language.php语言:在确定用户使用哪种语言时,我使用 PHP 来检查用户浏览器中设置的语言: http : //www.php.net/manual/en/function.http-negotiate-language.php

在我工作的一个小型电子商务网站上,我们使用 Akamai 提供地理定位数据,这些数据将根据访问者所处的状态提供定制的广告和价格。查看此了解更多信息: http : //www.akamai.com/ html/technology/products/personalization.html

IPGeolocation would be a better option for you. IPGeolocation将是您更好的选择。 They build their own database and update their database twice a week.他们建立自己的数据库并每周更新两次数据库。 They have 99% accuracy at the country level and 75% at the city level.他们在国家层面有 99% 的准确率,在城市层面有 75% 的准确率。 They also offer paid as well as free (1000 requests/day) API plans that you can see here: https://ipgeolocation.io/pricing.html .他们还提供付费和免费(每天 1000 个请求)API 计划,您可以在此处查看: https : //ipgeolocation.io/pricing.html

To facilitate the developers, IPGeolocation API offers some SDKs for various programming languages:为方便开发者,IPGeolocation API 提供了一些针对各种编程语言的 SDK:

The endpoint to get geolocation information of an IP address:获取 IP 地址的地理位置信息的端点:
This endpoint is meant to be called from the server side.此端点旨在从服务器端调用。

$ curl 'https://api.ipgeolocation.io/ipgeo?apiKey=API_KEY&ip=8.8.8.8'

The JSON response will be: 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
    }
}

You can also filter specific parameters from the entire response.您还可以从整个响应中过滤特定参数。

After getting the visitor's location, you can also redirect him to the page according to his language/currency by using this simple code:获取访问者的位置后,您还可以使用以下简单代码将他重定向到根据他的语言/货币的页面:

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(); 

I hope it would be the best option for you.我希望这对你来说是最好的选择。

I've been working on a project that requires geolocation, unfortunately I find that most services are working on MaxMind database , at last I used freegeoip , free and so far precise in my region.我一直在做一个需要地理定位的项目,不幸的是我发现大多数服务都在MaxMind数据库上工作,最后我使用了freegeoip ,免费并且在我所在的地区到目前为止是精确的。

Regards.问候。

For the language, it should come through from the browser.对于语言,它应该来自浏览器。 That's more useful and more accurate than geolocation.这比地理定位更有用、更准确。

For example, if you were traveling with your laptop and you used the internet, you'd probably want to see things in your language than the native language of the country you were in.例如,如果您携带笔记本电脑旅行并使用互联网,您可能希望以您的语言而不是您所在国家/地区的母语来查看内容。

Look at this question for more information: Checking browser's language by PHP?查看此问题以获取更多信息: Checking browser's language by PHP?

Another option is the http://ipinfo.io API (my own service).另一种选择是http://ipinfo.io API(我自己的服务)。 Here's an example of what the API returns.以下是 API 返回内容的示例。

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "CA",
  "country": "US",
  "phone": 650
}

You can also query for specific fields, such as country in this example:您还可以查询特定字段,例如本示例中的国家/地区:

$ curl ipinfo.io/8.8.8.8/country
US

More details are available at http://ipinfo.io/developers .更多详细信息可在http://ipinfo.io/developers 获得 It's free for up to 1,000 requests per day and you can signup to a paid plan beyond that (see http://ipinfo.io/pricing )每天最多可免费处理 1,000 个请求,您还可以注册超出此范围的付费计划(请参阅http://ipinfo.io/pricing

As I mentioned we built https://ipdata.co for this exact usecase正如我提到的,我们为这个确切的用例构建了https://ipdata.co

$ip = '78.8.53.5';
$details = json_decode(file_get_contents("https://api.ipdata.co/{$ip}"));

This will return这将返回

{
    "ip": "78.8.53.5",
    "city": "G\u0142og\u00f3w",
    "region": "Lower Silesia",
    "country_name": "Poland",
    "country_code": "PL",
    "continent_name": "Europe",
    "continent_code": "EU",
    "latitude": 51.663,
    "longitude": 16.0757,
    "asn": "AS12741",
    "organisation": "Netia SA",
    "postal": "67-200",
    "currency": "PLN",
    "currency_symbol": "z\u0142",
    "calling_code": "48",
    "flag": "https://ipdata.co/flags/pl.png",
    "time_zone": "Europe/Warsaw"
}

This has a number of Ecommerce relevant datapoints, specifically这有许多电子商务相关的数据点,特别是

  • Currency Symbol货币符号
  • Currency Code货币代码
  • Country Flag Icon国家国旗图标
  • Country Calling Code国家/地区电话代码

The currency symbol z\ł is ASCII for the Zloty货币符号z\ł是兹罗提的 ASCII

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM