Get location from an IP address in PHP
📅July 22, 2020
🗁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.
http://www.geoplugin.net/json.gp?ip=<ip_address>
</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.
<!--?php<p-->
// 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'];
?>
<p>You will get the following output in the browser.</p>
<!-- /wp:html -->
<!-- wp:image {"id":2379,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://www.cluemediator.com/wp-content/uploads/2020/07/output-get-location-from-an-ip-address-in-php-clue-mediator.png" alt="Get location information using geoPlugin API - Clue Mediator" class="wp-image-2379"><figcaption>Get location information using geoPlugin API - Clue Mediator</figcaption></figure>
<!-- /wp:image -->
<!-- wp:html -->
<p>Also you can process it with a PHP cURL request.</p>
<pre><!--?php<p-->
$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);
?>
</pre>
<p>The below output that you can see in the browser.</p>
<!-- /wp:html -->
<!-- wp:image {"id":2380,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://www.cluemediator.com/wp-content/uploads/2020/07/output-curl-get-location-from-an-ip-address-in-php-clue-mediator-1024x459.png" alt="Get location information using CURL request - Clue Mediator" class="wp-image-2380"><figcaption>Get location information using CURL request - Clue Mediator</figcaption></figure>
<!-- /wp:image -->
<!-- wp:html -->
<h3 id="uia">2. Using ipinfo API</h3>
<p>Now, let’s use another way to get the geo information using <a href="https://ipinfo.io/" title="ipinfo" target="_blank" rel="nofollow noopener noreferrer">ipinfo</a> 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 <a href="https://ipinfo.io/pricing" title="checkout more" target="_blank" rel="nofollow noopener noreferrer">purchase a plan</a>.</p>
<p>Use the below API in a PHP file.</p>
<pre>https://ipinfo.io/<ip_address>/json
</ip_address></pre>
<p>Same as the above method we have to use the ipinfo API to get the location information.</p>
<pre><!--?php<p-->
// 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>";
?>
</pre>
<p>Check out the output in the browser.</p>
<!-- /wp:html -->
<!-- wp:image {"id":2381,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://www.cluemediator.com/wp-content/uploads/2020/07/output-ipinfo-get-location-from-an-ip-address-in-php-clue-mediator.png" alt="Get location information using ipinfo API - Clue Mediator" class="wp-image-2381"><figcaption>Get location information using ipinfo API - Clue Mediator</figcaption></figure>
<!-- /wp:image -->
<!-- wp:html -->
<p>That’s it for today.<br>Thank you for reading. Happy Coding..!!</p>
<!-- /wp:html --></pre></pre></pre>