Geolocation in PHP

Asked

Viewed 2,138 times

0

Hello, I wonder if there’s a way I can get the location of a user who accesses my application in PHP, like, return something like this:

Country: Brazil State: bláblá City: Narnia

and if possible that still gave the attitude and longitude

1 answer

4

You can use the API of this website. Apparently the free version allows up to 1000 daily requests. With PHP, the code can be quite simple:

$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
var_dump($details);

The result is something like:

object(stdClass)#1 (7) {
  ["ip"]=> string(13) "..."
  ["hostname"]=> string(11) "No Hostname"
  ["city"]=> string(8) "Curitiba"
  ["region"]=> string(6) "Parana"
  ["country"]=> string(2) "BR"
  ["loc"]=> string(17) "..."
  ["org"]=> string(30) "AS18881 TELEFÔNICA BRASIL S.A"
}

Some data were omitted for security reasons.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.