How to get Geolocation using PHP cURL from IP Address

Webs Codex
2 min readAug 23, 2023

--

What is Geolocation

Geolocation is a method for mapping the virtual identity of an internet-connected device to its physical address. A virtual identity is represented by an Internet Protocol (IP) address. The physical address is the location of this device within the geographic area of ​​the earth, represented by latitude and longitude. It can still be extrapolated to a geopolitical or postal or zip code address that we use on a daily basis to identify a place.

See also :

How do you find Geolocation?

On your computer, open Google Maps. Right-click the place or area on the map. This will open a pop-up window. You can find your latitude and longitude in decimal format at the top.

How to get Geolocation using PHP-cURL from IP Address

Geolocation refers to the identification of the geographical location of a user or computer device.

In this tutorials, we will create a web page where the user can enter the IP Address of any device, and then the server will provide Geolocation of the IP address fetching the following details using the IP Geolocation API.

  • Country Name
  • Region
  • City Name
  • Zip code
  • IP Address
  • Latitude
  • Longitude

Approach:

  • Call API via HTTP GET request using cURL in PHP.
  • Convert API JSON response to array using PHP json_decode() function.
  • Retrieve IP data from API response.

Example: The following code gets the location from IP address using PHP cURL.

index.php

<!DOCTYPE html>
<html>
<head>
<title>How to get Geolocation using PHP-cURL from IP Addres</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h2 class="text-success text-center mb-4">How to get Geolocation using PHP-cURL from IP Addres</h2>
<div class="row">
<div class="col-md-6 offset-3">
<form method='post' enctype='multipart/form-data'>
<div class="mb-3">
<label for="ipAddress">Give IP address for check location</label>
<input type='text' name='ipAddress' class="form-control" placeholder="User IP address" id="ipAddress" />
</div>
<div class="mb-3 mt-3">
<button type="submit" class="btn btn-primary" name='submit'>Submit</button>
</div>
</form>
</div>
<?php

if(isset($_POST['submit']))
{

$userIP = $_POST['ipAddress'];

//$apiURL = 'https://api.ipgeolocationapi.com/geolocate/'.$userIP;
$apiURL = "http://ipinfo.io/{$userIP}/json";

$ch = curl_init($apiURL);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($ch);

curl_close($ch);

$ipResult = json_decode($apiResponse);

}

?>

<?php if (!empty($ipResult)) { ?>
<table class="table table-striped">
<thead>
<tr>
<th>Country</th>
<th>Region</th>
<th>City</th>
<th>Zip code</th>
<th>Latitude</th>
<th>Longitude</th>
<th>IP Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $ipResult->country ?></td>
<td><?php echo $ipResult->region ?></td>
<td><?php echo $ipResult->city ?></td>
<td><?php echo $ipResult->postal ?></td>
<?php
$latLonArr = explode(",", $ipResult->loc);
?>
<td><?php echo $latLonArr[0] ?></td>
<td><?php echo $latLonArr[1] ?></td>
<td><?php echo $ipResult->ip ?></td>
</tr>
</tbody>
</table>
<?php } ?>
</div>
</div>
</body>
</html>

If you want to more article then click on the click below.

How to get Geolocation using PHP cURL from IP Address

--

--

Webs Codex
Webs Codex

Written by Webs Codex

Webs Codex is programming and web development blog

No responses yet