get region/city from ip

michele picture michele · Jun 14, 2011 · Viewed 65.6k times · Source

how can I retrieve the region and the city from an ip? I have found this service: http://api.hostip.info/?ip=xyz.qwe.rty

But It doesn't give me accurate info like this: http://www.ipaddresslocation.org/

Do you know some free service? I need to retrieve this info with a php script, but I wouldn't install external library.

Thank you so much

Answer

Ben Dowling picture Ben Dowling · Jul 14, 2013

http://ipinfo.io, my service, gives you city, country and other related information about an IP:

$ 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
}

Here's a PHP example:

$details = json_decode(file_get_contents("http://ipinfo.io/".$_SERVER['REMOTE_ADDR']"));
echo $details->city; // -> "Mountain View"