Sometimes, we want to get the location from an IP address with PHP.
In this article, we’ll look at how to get the location from an IP address with PHP.
How to get the location from an IP address with PHP?
To get the location from an IP address with PHP, we can use a web API.
For instance, we write
$location = file_get_contents(
"http://freegeoip.net/json/" . $_SERVER["REMOTE_ADDR"]
);
print_r($location);
to call file_get_contents
to make a get request to "http://freegeoip.net/json/" . $_SERVER["REMOTE_ADDR"]
.
$_SERVER["REMOTE_ADDR"]
has the IP address of the client that made the request.
Then we get the $location
response array.
It includes the country, city, zip code, latitude, and longitude data.
Conclusion
To get the location from an IP address with PHP, we can use a web API.