How to get country & geo location from IP address using PHP

This is a quick and easy tutorial on how to get the country name as well as latitude and longitude from an IP address using just a few lines of code. You will be surprised at how simple it is to locate ip address using PHP and to make it even more awesome, we’ll construct a link to Google Maps with our geo coordinates so you can see the location of your IP on a map.

Geolocation from IP address

We could search for a free geo location database and use it on our server locally and maybe, just maybe get the data a few milliseconds faster, but in reality a third party API will do just fine and our server has to spend slightly less processing power on the task.

There are quite a few geo location API’s out there and some work better than others. For example I tried, but the result landed me more than 100 miles in the wrong direction so after testing with various services, I ended up with which is pretty accurate.

You can check out the documentation yourself, but for our tutorial this is all we need:

$details = json_decode(file_get_contents("$ip"));

Now we have your visitor’s IP address in a variable and we can use it to get a JSON object back from IP-API’s server that will be stored as $details.

This is how the response will look like:

"status": "success",
"country": "COUNTRY",
"countryCode": "COUNTRY CODE",
"region": "REGION CODE",
"regionName": "REGION NAME",
"city": "CITY",
"zip": "ZIP CODE",
"lat": LATITUDE,
"timezone": "TIME ZONE",
"isp": "ISP NAME",
"as": "AS NUMBER / NAME",

As you can see it’s very manageable and you can echo various bits of information out like this:

echo $details->country;

Latitude and Longitude from IP address

When you look above at the IP-API’s response, it becomes clear that in order to put both Latitude and Longitude in variables that we can use later throughout our script, all we need to do is this:

$lat = $details->lat;
$lon = $details->lon;

If you like, you can use other bits of data from the same response. For example I will use the country and countryCode in my script:

$country = $details->country;
$countryCode = $details->countryCode;

Latitude and Longitude in Google Maps URL

The structure of a basic Google Maps query URL is very simple and straightforward consisting only of the URL to Maps and a q variable with your comma separated latitude and longitude. This is how it looks when using our previously set $lat and $lon variables:$lat,$lon

Putting it all together

This is the whole script you need to use in order to output something similar to the screenshot at the bottom. I have added some basic layout for the output by separating the country and countryCode with a comma as well as putting Lat and Lon on a new line followed by a URL to Google Maps:

// Credits:
$details = json_decode(file_get_contents("$ip"));
$lat = $details->lat;
$lon = $details->lon;
$country = $details->country;
$countryCode = $details->countryCode;
echo "Country: $country, $countryCode<br>
Lat: $lat Lon: $lon<br>
<a href='$lat,$lon' target='_blank'>Locate on Google Maps</a>";

Get geolocation from IP address with PHP latitude and longitude Google Maps URLThe above script will output something similar to what you see on the left and with a little CSS magic you can make it look and feel a lot nicer, but I’ll leave that for next time.

Thank you for reading and I hope you found this tutorial useful!