<p>Does anyone know any simple way to retrieve the country from a given IP Address, preferably in ISO_3166-1 format?</p>

[{'answer_id': 1034, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 4, 'selected': False, 'text': '<p>Here\'s a nice free service with a public API:\n<a href="http://www.hostip.info/use.html" rel="noreferrer">http://www.hostip.info/use.html</a></p>\n'}, {'answer_id': 1056, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 6, 'selected': True, 'text': '<p>A lot of people (including my company) seem to use MaxMind GeoIP.</p>\n\n<p>They have a free version <a href="http://dev.maxmind.com/geoip/geoip2/geolite2/" rel="noreferrer">GeoLite</a> which is not as accurate as the paid version, but if you\'re just after something simple, it may be good enough.</p>\n'}, {'answer_id': 1257, 'author': 'Chris Miller', 'author_id': 206, 'author_profile': 'https://Stackoverflow.com/users/206', 'pm_score': 3, 'selected': False, 'text': '<p>I don\'t know how accurate <a href="http://hostip.info" rel="nofollow noreferrer">http://hostip.info</a> site is. I just visited 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 its guess, but if you are using this service to track web site visitors 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.</p>\n<p>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.</p>\n'}, {'answer_id': 66140, 'author': 'Warren Blanchet', 'author_id': 1054, 'author_profile': 'https://Stackoverflow.com/users/1054', 'pm_score': 5, 'selected': False, 'text': '<p>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.</p>\n\n<p>For services:</p>\n\n<ul>\n<li><a href="http://www.hostip.info/use.html" rel="nofollow noreferrer">http://www.hostip.info/use.html</a> (as mentioned by <a href="https://stackoverflow.com/questions/1033/ip-to-country#1034">Mark</a>)</li>\n<li><a href="http://www.team-cymru.org/Services/ip-to-asn.html" rel="nofollow noreferrer">http://www.team-cymru.org/Services/ip-to-asn.html</a></li>\n</ul>\n\n<p>For lists:</p>\n\n<ul>\n<li><a href="http://www.maxmind.com/app/geoip_country" rel="nofollow noreferrer">http://www.maxmind.com/app/geoip_country</a> (as mentioned by <a href="https://stackoverflow.com/questions/1033/ip-to-country#1056">Orion</a>)</li>\n<li><p>You could roll your own by downloading the lists from the RIRs:</p>\n\n<ul>\n<li><a href="ftp://ftp.arin.net/pub/stats/arin/delegated-arin-latest" rel="nofollow noreferrer">ftp.arin.net/pub/stats/arin/delegated-arin-latest</a></li>\n<li><a href="ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest" rel="nofollow noreferrer">ftp.ripe.net/ripe/stats/delegated-ripencc-latest</a></li>\n<li><a href="ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest" rel="nofollow noreferrer">ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest</a></li>\n<li><a href="ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest" rel="nofollow noreferrer">ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest</a></li>\n<li><p><a href="ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest" rel="nofollow noreferrer">ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest</a></p>\n\n<p>The format is documented <a href="ftp://ftp.arin.net/pub/stats/arin/README" rel="nofollow noreferrer">in this README</a></p></li>\n</ul></li>\n</ul>\n'}, {'answer_id': 142851, 'author': 'CSharpAtl', 'author_id': 11907, 'author_profile': 'https://Stackoverflow.com/users/11907', 'pm_score': 2, 'selected': False, 'text': '<p>The most accurate is <a href="http://www.digital-element.net" rel="nofollow noreferrer">Digital Elements</a> NetAcuity. It\'s not free but you get what you pay for most of the time.</p>\n'}, {'answer_id': 2056811, 'author': 'Donny Kurnia', 'author_id': 141177, 'author_profile': 'https://Stackoverflow.com/users/141177', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://ipinfodb.com/" rel="noreferrer">ipinfodb</a> 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.</p>\n'}, {'answer_id': 2056874, 'author': 'Niklas Rosencrantz', 'author_id': 108207, 'author_profile': 'https://Stackoverflow.com/users/108207', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://code.google.com/p/gmaps-samples/source/browse/trunk/clientlocation/clientlocation.html" rel="nofollow noreferrer">google\'s</a> clientlocation returns (<a href="http://www.gralumo.com" rel="nofollow noreferrer">my example</a>)</p>\n\n<pre><code>latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);\nlocation = "IP location: " + getFormattedLocation();\ndocument.getElementById("location").innerHTML = location;\n</code></pre>\n'}, {'answer_id': 7822524, 'author': 'zak', 'author_id': 249816, 'author_profile': 'https://Stackoverflow.com/users/249816', 'pm_score': 2, 'selected': False, 'text': '<p>You can use the solution provided for <a href="https://stackoverflow.com/questions/7778844/how-to-program-different-welcome-pages-based-on-country-ip">this question</a>.</p>\n\n<p>But it returns a 2 digit country code.</p>\n'}, {'answer_id': 11717563, 'author': 'pckabeer', 'author_id': 1298003, 'author_profile': 'https://Stackoverflow.com/users/1298003', 'pm_score': 2, 'selected': False, 'text': '<p>Try this php code</p>\n\n<pre><code>  &lt;?php  $ip = $_SERVER[\'REMOTE_ADDR\'];\n    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&amp;full=true");\n    $json = json_decode($json,true);\n    $timezone = $json[localTimeZone];?&gt;\n</code></pre>\n'}, {'answer_id': 17350045, 'author': 'saifur', 'author_id': 1942132, 'author_profile': 'https://Stackoverflow.com/users/1942132', 'pm_score': 2, 'selected': False, 'text': '<p>use the function ipToCountry($ip) from <a href="http://www.mmtutorialvault.com/php-ip-to-country-function/" rel="nofollow">http://www.mmtutorialvault.com/php-ip-to-country-function/</a></p>\n'}, {'answer_id': 23872935, 'author': 'user3463375', 'author_id': 3463375, 'author_profile': 'https://Stackoverflow.com/users/3463375', 'pm_score': 2, 'selected': False, 'text': "<p>you can use web service API's which do this work like:</p>\n\n<pre><code>see example of service: http://ip-api.com and usage: http://whatmyip.info\n</code></pre>\n"}, {'answer_id': 25498255, 'author': 'Ben Dowling', 'author_id': 36191, 'author_profile': 'https://Stackoverflow.com/users/36191', 'pm_score': 2, 'selected': False, 'text': '<p>You can use my service, <a href="http://ipinfo.io" rel="nofollow">http://ipinfo.io</a>, for this. The API returns a whole bunch of different details about an IP address:</p>\n\n<pre><code>$ curl ipinfo.io/8.8.8.8\n{\n  "ip": "8.8.8.8",\n  "hostname": "google-public-dns-a.google.com",\n  "loc": "37.385999999999996,-122.0838",\n  "org": "AS15169 Google Inc.",\n  "city": "Mountain View",\n  "region": "CA",\n  "country": "US",\n  "phone": 650\n}\n</code></pre>\n\n<p>If you\'re only after the country code you just need to add /country to the URL:</p>\n\n<pre><code>$ curl ipinfo.io/8.8.8.8/country\nUS\n</code></pre>\n\n<p>Here\'s a generic PHP function you could use:</p>\n\n<pre><code>function ip_details($ip) {\n    $json = file_get_contents("http://ipinfo.io/{$ip}");\n    $details = json_decode($json);\n    return $details;\n}\n\n$details = ip_details("8.8.8.8");\n\necho $details-&gt;city;     // =&gt; Mountain View\necho $details-&gt;country;  // =&gt; US\necho $details-&gt;org;      // =&gt; AS15169 Google Inc.\necho $details-&gt;hostname; // =&gt; google-public-dns-a.google.com\n</code></pre>\n\n<p>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 <code>$_SERVER[\'REMOTE_ADDR\']</code> instead. More details are available at <a href="http://ipinfo.io/developers" rel="nofollow">http://ipinfo.io/developers</a></p>\n'}, {'answer_id': 48855469, 'author': 'Jonathan', 'author_id': 3176550, 'author_profile': 'https://Stackoverflow.com/users/3176550', 'pm_score': 1, 'selected': False, 'text': '<p>See <a href="https://ipdata.co" rel="nofollow noreferrer">ipdata.co</a> which gives you several data points from an IP address.</p>\n<p>The API is pretty fast, with 10 global endpoints each being able to handle &gt;800M calls daily.</p>\n<p>Here\'s a curl example:</p>\n<pre><code>curl https://api.ipdata.co/78.8.53.5\n{\n    &quot;ip&quot;: &quot;78.8.53.5&quot;,\n    &quot;city&quot;: &quot;G\\u0142og\\u00f3w&quot;,\n    &quot;region&quot;: &quot;Lower Silesia&quot;,\n    &quot;region_code&quot;: &quot;DS&quot;,\n    &quot;country_name&quot;: &quot;Poland&quot;,\n    &quot;country_code&quot;: &quot;PL&quot;,\n    &quot;continent_name&quot;: &quot;Europe&quot;,\n    &quot;continent_code&quot;: &quot;EU&quot;,\n    &quot;latitude&quot;: 51.6461,\n    &quot;longitude&quot;: 16.1678,\n    &quot;asn&quot;: &quot;AS12741&quot;,\n    &quot;organisation&quot;: &quot;Netia SA&quot;,\n    &quot;postal&quot;: &quot;67-200&quot;,\n    &quot;currency&quot;: &quot;PLN&quot;,\n    &quot;currency_symbol&quot;: &quot;z\\u0142&quot;,\n    &quot;calling_code&quot;: &quot;48&quot;,\n    &quot;flag&quot;: &quot;https://ipdata.co/flags/pl.png&quot;,\n    &quot;emoji_flag&quot;: &quot;\\ud83c\\uddf5\\ud83c\\uddf1&quot;,\n    &quot;time_zone&quot;: &quot;Europe/Warsaw&quot;,\n    &quot;is_eu&quot;: true,\n    &quot;suspicious_factors&quot;: {\n        &quot;is_tor&quot;: false\n    }\n}⏎  \n</code></pre>\n'}, {'answer_id': 49001745, 'author': 'Vlam', 'author_id': 6647585, 'author_profile': 'https://Stackoverflow.com/users/6647585', 'pm_score': 1, 'selected': False, 'text': '<p>You can try the free <a href="https://lite.ip2location.com/database/ip-country" rel="nofollow noreferrer">IP2Location LITE database</a></p>\n\n<p>To create the table in MySQL</p>\n\n<pre><code>CREATE DATABASE ip2location;\nUSE ip2location;\nCREATE TABLE `ip2location_db1`(\n    `ip_from` INT(10) UNSIGNED,\n    `ip_to` INT(10) UNSIGNED,\n    `country_code` CHAR(2),\n    `country_name` VARCHAR(64),\n    INDEX `idx_ip_to` (`ip_to`)\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;\n</code></pre>\n\n<p>To import the data</p>\n\n<pre><code>LOAD DATA LOCAL\n    INFILE \'IP2LOCATION-LITE-DB1.CSV\'\nINTO TABLE\n    `ip2location_db1`\nFIELDS TERMINATED BY \',\'\nENCLOSED BY \'"\'\nLINES TERMINATED BY \'\\r\\n\'\nIGNORE 0 LINES;\n</code></pre>\n\n<p>PHP code to query the MySQL</p>\n\n<pre><code>&lt;?php\n// Replace this MYSQL server variables with actual configuration\n$mysql_server = "mysql_server.com";\n$mysql_user_name = "UserName";\n$mysql_user_pass = "Password";\n\n// Retrieve visitor IP address from server variable REMOTE_ADDR\n$ipaddress = $_SERVER["REMOTE_ADDR"];\n\n// Convert IP address to IP number for querying database\n$ipno = Dot2LongIP($ipaddress);\n\n// Connect to the database server\n$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");\n\n// Connect to the IP2Location database\nmysql_select_db("ip2location") or die("Could not select database");\n\n// SQL query string to match the recordset that the IP number fall between the valid range\n$query = "SELECT * FROM ip2location_db1 WHERE $ipno &lt;= ip_to LIMIT 1";\n\n// Execute SQL query\n$result = mysql_query($query) or die("IP2Location Query Failed");\n\n// Retrieve the recordset (only one)\n$row = mysql_fetch_object($result);\n\n// Keep the country information into two different variables\n$country_code = $row-&gt;country_code;\n$country_name = $row-&gt;country_name;\n\necho "Country_code: " . $country_code . "&lt;br/&gt;";\necho "Country_name: " . $country_name . "&lt;br /&gt;";\n\n// Free recordset and close database connection\nmysql_free_result($result);\nmysql_close($link);\n\n// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)\nfunction Dot2LongIP ($IPaddr) {\n if ($IPaddr == "")\n {\n   return 0;\n } else {\n   $ips = explode(".", $IPaddr);\n   return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);\n }\n}\n?&gt;\n</code></pre>\n'}, {'answer_id': 65161079, 'author': 'bre_dev', 'author_id': 1378258, 'author_profile': 'https://Stackoverflow.com/users/1378258', 'pm_score': -1, 'selected': False, 'text': '<p>You can give a try to <a href="https://astroip.co" rel="nofollow noreferrer">https://astroip.co</a>, it is a new Geolocation API I built which exposes geo data together with other useful datapoints like currency, timezone, ASN data and security.</p>\n<p>Here it is an example of the json response:</p>\n<pre><code>curl https://api.astroip.co/70.163.7.1\n{\n  &quot;status_code&quot;: 200,\n  &quot;geo&quot;: {\n    &quot;is_metric&quot;: false,\n    &quot;is_eu&quot;: false,\n    &quot;longitude&quot;: -77.0924,\n    &quot;latitude&quot;: 38.7591,\n    &quot;country_geo_id&quot;: 6252001,\n    &quot;zip_code&quot;: &quot;22306&quot;,\n    &quot;city&quot;: &quot;Alexandria&quot;,\n    &quot;region_code&quot;: &quot;VA&quot;,\n    &quot;region_name&quot;: &quot;Virginia&quot;,\n    &quot;continent_code&quot;: &quot;NA&quot;,\n    &quot;continent_name&quot;: &quot;North America&quot;,\n    &quot;capital&quot;: &quot;Washington&quot;,\n    &quot;country_name&quot;: &quot;United States&quot;,\n    &quot;country_code&quot;: &quot;US&quot;\n  },\n  &quot;asn&quot;: {\n    &quot;route&quot;: &quot;70.160.0.0/14&quot;,\n    &quot;type&quot;: &quot;isp&quot;,\n    &quot;domain&quot;: &quot;cox.net&quot;,\n    &quot;organization&quot;: &quot;ASN-CXA-ALL-CCI-22773-RDC&quot;,\n    &quot;asn&quot;: &quot;AS22773&quot;\n  },\n  &quot;currency&quot;: {\n    &quot;native_name&quot;: &quot;US Dollar&quot;,\n    &quot;code&quot;: &quot;USD&quot;,\n    &quot;name&quot;: &quot;US Dollar&quot;,\n    &quot;symbol&quot;: &quot;$&quot;\n  },\n  &quot;timezone&quot;: {\n    &quot;is_dst&quot;: false,\n    &quot;gmt_offset&quot;: -18000,\n    &quot;date_time&quot;: &quot;2020-12-05T17:04:48-05:00&quot;,\n    &quot;microsoft_name&quot;: &quot;Eastern Standard Time&quot;,\n    &quot;iana_name&quot;: &quot;America/New_York&quot;\n  },\n  &quot;security&quot;: {\n    &quot;is_crawler&quot;: false,\n    &quot;is_proxy&quot;: false,\n    &quot;is_tor&quot;: false,\n    &quot;tor_insights&quot;: null,\n    &quot;proxy_insights&quot;: null,\n    &quot;crawler_insights&quot;: null\n  },\n  &quot;error&quot;: null,\n  &quot;ip_type&quot;: &quot;ipv4&quot;,\n  &quot;ip&quot;: &quot;70.163.7.1&quot;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/1033', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/87/']