简体   繁体   中英

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. 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. We already have a solution for working out which currency to use provided we can obtain the ISO code of the visitor's country.



Thought I'd leave a rough explanation of the outcome for anyone who visits this question.

I've used freegoip for detecting user location as suggested in this thread.我已经使用 freegoip 来检测用户位置,如该线程中建议的那样。
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. 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. They also offer paid as well as free (1000 requests/day) API plans that you can see here: https://ipgeolocation.io/pricing.html .

To facilitate the developers, IPGeolocation API offers some SDKs for various programming languages:

The endpoint to get geolocation information of an IP address:
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:

{
    "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.

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?

Another option is the http://ipinfo.io API (my own service). Here's an example of what the API returns.

$ 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 . 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 )

As I mentioned we built https://ipdata.co for this exact usecase

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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