Get location from an IP address in PHP
Today we’ll show you how to get location from an IP address in PHP. In this article, we will use the third party free API to get the geolocation (like country, state, city, latitude, longitude, timezone, currency, etc.) based on the IP address.
Check out the more articles in PHP.
Way to get location from an IP address
1. Using geoPlugin API
Here we will use the following geoPlugin third party API to get the geo information.
1 | http://www.geoplugin.net/json.gp?ip=<IP_ADDRESS> |
Let’s use the above API with a static IP address to implement in a PHP file. After the API call, we need to use the file_get_contents()
function to get the requested IP data in the JSON format. Then we will convert the JSON data to an array using the json_decode()
function.
This is a free API and we can use it as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php // static IP address $ip = "193.37.252.21"; // API with IP address $url = "http://www.geoplugin.net/json.gp?ip=".$ip; // get a requested data $userInfo = file_get_contents($url); // convert JSON to array $result = json_decode($userInfo,true); // you can print the array to see the all information // echo "<pre>"; // print_r($result); echo "<b>IP Address : </b>".$result['geoplugin_request']."<br>"; echo "<b>City : </b>".$result['geoplugin_city']."<br>"; echo "<b>State : </b>".$result['geoplugin_regionName']."<br>"; echo "<b>Country : </b>".$result['geoplugin_countryName']."<br>"; echo "<b>Country Code : </b>".$result['geoplugin_countryCode']."<br>"; echo "<b>Latitude : </b>".$result['geoplugin_latitude']."<br>"; echo "<b>Longitude : </b>".$result['geoplugin_longitude']."<br>"; echo "<b>Timezone : </b>".$result['geoplugin_timezone']; ?> |
You will get the following output in the browser.
Also you can process it with a PHP cURL request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php $ip = "193.37.252.21"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "http://www.geoplugin.net/json.gp?ip=".$ip, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "Accept: application/json", "Cache-Control: no-cache", ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { $result = json_decode($response,true); } echo "<pre>"; print_r($result); ?> |
The below output that you can see in the browser.
2. Using ipinfo API
Now, let’s use another way to get the geo information using ipinfo API. It provides geolocation data with 50,000 lookups per month for non-commercial projects, if you want more than it then you have to purchase a plan.
Use the below API in a PHP file.
1 | https://ipinfo.io/<IP_ADDRESS>/json |
Same as the above method we have to use the ipinfo API to get the location information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php // static IP address $ip = "193.37.252.21"; // API with IP address $url = "https://ipinfo.io/".$ip."/json"; // get a requested data $userInfo = file_get_contents($url); // convert JSON to array $result = json_decode($userInfo,true); // you can print the array to see the all information // echo "<pre>"; // print_r($result); echo "<b>IP Address : </b>".$result['ip']."<br>"; echo "<b>City : </b>".$result['city']."<br>"; echo "<b>State : </b>".$result['region']."<br>"; echo "<b>Country Code : </b>".$result['country']."<br>"; echo "<b>Latitude & Longitude : </b>".$result['loc']."<br>"; echo "<b>Timezone : </b>".$result['timezone']."<br>"; ?> |
Check out the output in the browser.
That’s it for today.
Thank you for reading. Happy Coding..!!