Does anyone know of a simple way to retrieve the country for a given IP Address? Preferably in ISO_3166-1 format?
original title: "lookup - How to get the Country according to a certain IP?"
Does anyone know of a simple way to retrieve the country for a given IP Address? Preferably in ISO_3166-1 format?
Qualcuno sa un modo semplice per recuperare il paese per un determinato indirizzo IP? Preferibilmente in formato ISO_3166-1?
Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"
A lot of people (including my company) seem to use MaxMind GeoIP.
They have a free version GeoLite which is not as accurate as the paid version, but if you're just after something simple, it may be good enough.
There are two approaches: using an Internet service and using some kind of local list (perhaps wrapped in a library). What you want will depend on what you are building.
For services:
For lists:
You could roll your own by downloading the lists from the RIRs:
ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest
The format is documented in this README
Here's a nice free service with a public API: http://www.hostip.info/use.html
ipinfodb provides a free database and API for IP to country and vice versa. They use free data from MaxMind. The data gets updated every month, and it's a great free alternative with decent accuracy.
I don't know how accurate that hostip.info site is. I just visted that site, and it reported that my country is Canada. I'm in the US and the ISP that my office uses only operates from the US. It does allow you to correct it's guess, but if you are using this service to track web site vistors by country, you'll have no way of knowing if the data is correct. Of course, I'm just one data point. I downloaded the GeoLite Country database, which is just a .csv file, and my IP address was correctly identified as US.
Another benefit of the MaxMind product line (paid or free) is that you have the data, you don't incur the performance hit of making a web service call to another system.
The most accurate is Digital Elements NetAcuity...not free but you get what you pay for most of the time....Digital Element
google's clientlocation returns (my example)
You can use the solution provided for this question.
But it returns a 2 digit country code.
Try this php code
You can use my service, http://ipinfo.io, for this. The API returns a whole bunch of different details about an IP address:
If you're only after the country code you just need to add /country to the URL:
Here's a generic PHP function you could use:
I've used the IP 8.8.8.8 in these examples, but if you want details for the user's IP just pass in
$_SERVER['REMOTE_ADDR']
instead. More details are available at http://ipinfo.io/developersuse the function ipToCountry($ip) from http://www.mmtutorialvault.com/php-ip-to-country-function/
you can use web service API's which do this work like:
See ipdata.co which gives you several data points from an ip address.
The API is pretty fast, with 10 global endpoints each able to handle >800M calls daily.
Here's a curl example;
You can try the free IP2Location LITE database
To create the table in MySQL
To import the data
PHP code to query the MySQL