For those who can't be bothered digging, uses <a href="http://ip-api.com/json/" rel="nofollow">http://ip-api.com/json/</a><p>For those who don't want their web service to rely on other web services, I recommend MaxMind's lite database, they have a fairly recent downloadable binary of IP to geographic coodinates.
I've been using <a href="http://freegeoip.net/" rel="nofollow">http://freegeoip.net/</a><p>It's not perfect, but it seems to be good enough for my purposes.<p><pre><code> $ip = $_SERVER['REMOTE_ADDR'];
$ua = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "freegeoip.net/csv/" . $ip);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$ip = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$array = explode(',', $ip);</code></pre>
A while back I needed a no-cruft way to get my IP reliably, so put this in my nginx config, to allow "curl example.com/ip":<p>http {<p><pre><code> # ...
perl_modules /etc/nginx/perl;
perl_require myip.pm;
# ...
server {
# ...
location = /ip {
perl myip::handler;
}
# ...
}</code></pre>
}<p>then in /etc/nginx/perl/myip.pm :<p>---<p>package myip;<p>use nginx;<p>sub handler {<p><pre><code> my $r = shift;
$r->send_http_header("text/plain");
return OK if $r->header_only;
$r->print($r->remote_addr() . "\n");
return OK;
</code></pre>
}<p>1;<p>---
Honestly, I have been around the block trying to find a decent company that provides either a database or api for looking up a location from an ip address.<p>It seems like every company from MaxMind to IP2Location and all of the others are accurate for some ip addresses, and severely inaccurate for others. And I think that will only become more and more common as all of the ipv4 addresses run out and there are hundreds to thousands behind the same ip address.<p>I've even tried crowdsourcing from four different ip to location sources and even then, sometimes all four return different cities and even states for a single ip address.<p>Is there any company out there that is actually fairly accurate (on a city basis)?
I recently noticed that you could create your own "IP -> countrycode" lookup system by using the delegation tables provided by arin/ripencc/afrinic/...<p>It might not be very accurate, but it's a fun little exercise:
<a href="http://blog.marc-seeger.de/2013/09/07/ip-to-countrycode-with-ruby/" rel="nofollow">http://blog.marc-seeger.de/2013/09/07/ip-to-countrycode-with...</a>
For a fast GeoIP JSON API, you can try Telize which is open source and has a public API : <a href="http://www.telize.com" rel="nofollow">http://www.telize.com</a><p>It is built on Nginx + Lua so it has very minimal overhead.<p>Source code is on GitHub : <a href="https://github.com/fcambus/telize" rel="nofollow">https://github.com/fcambus/telize</a>
In general, the accuracy of geolocation is at 70-80% at the city level. We have researched and compare the data from several providers such as IP2Location, Maxmind, Geobytes and so on. The IP address can be freely reallocated to any postcodes within one or multiple cities so we think that the 70-80% is acceptable.
Also available at <a href="http://viewdns.info/iplocation/" rel="nofollow">http://viewdns.info/iplocation/</a>. There's an API as well <a href="http://viewdns.info/api/" rel="nofollow">http://viewdns.info/api/</a>.
What is the database behind all these geo IP providers? I see many APIs providing same functionality and results are the same. Is this database purchased from somewhere every time you want to do this?
Simple and cool! I'm starting to learn python so I'll definitely be looking through the source, thanks for posting, flask looks like a nice little framework to get started on
If you want to take IP lookup one step further: <a href="https://github.com/fourtonfish/HelloSalut" rel="nofollow">https://github.com/fourtonfish/HelloSalut</a>