How to get Geolocation using PHP-cURL from IP Address

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


  • 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.

<!DOCTYPE html>
	<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="" rel="stylesheet">
	<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 class="mb-3 mt-3">
				  		<button type="submit" class="btn btn-primary" name='submit'>Submit</button>


					$userIP = $_POST['ipAddress'];

					//$apiURL = ''.$userIP;
					$apiURL = "{$userIP}/json";

					$ch = curl_init($apiURL);

					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

					$apiResponse = curl_exec($ch);


					$ipResult = json_decode($apiResponse);



			<?php if (!empty($ipResult)) { ?>
				<table class="table table-striped">
				        <th>Zip code</th>
				        <th>IP Address</th>
				      	<td><?php echo $ipResult->country ?></td>
				      	<td><?php echo $ipResult->region ?></td>
				        <td><?php echo $ipResult->city ?></td>
				        <td><?php echo $ipResult->postal ?></td>
				        	$latLonArr = explode(",", $ipResult->loc);
				        <td><?php echo $latLonArr[0] ?></td>
				        <td><?php echo $latLonArr[1] ?></td>
				        <td><?php echo $ipResult->ip ?></td>
			<?php } ?>


Valid IP address: When the user enters valid IP address.

How to get Geolocation using PHP-cURL from IP Address

You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions or problems about this tutorial, please comment on the form below.😊

Leave a Reply

Your email address will not be published. Required fields are marked *