In this tutorial, I'll cover how to Get Geolocation Country, Latitude, and Longitude from IP Address using PHP. This example code will get free Geolocation IP from API to get latitude, and longitude, location, country, region, city, from IP address using PHP code and Follows YouTube tutorial(CodeAT21) and GitHub repository full source code Available.

Get Location from IP Address

Use the Geolocation API via HTTP GET request and convert to JSON response to an array using json_decode() from the API response.

$userIP = $_SERVER['REMOTE_ADDR'];

$ip = '162.222.178.77'; 

$URL = 'https://freegeoip.app/json/'.$ip;
 

Next, Project structure your folder as follows.

Geolocation
├── index.php
├── index2.php
└── style.css 

Next, open up the index.php file located in the Geolocation folder. You should see this:

index.php
<?php $ipJsonInfo = array(); $statusMsg = ''; if(isset($_POST['submit'])){ $statusMsg = 'Geolocation Information:'; $userIP = $_POST['ip_address']; $ipJsonInfo = getIPLocation($userIP); } function getIPLocation($ip){ $addApiURL = 'https://freegeoip.app/json/'.$ip; // Make HTTP GET request using cURL $curl = curl_init($addApiURL); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $returnData = curl_exec($curl); if($returnData === FALSE) { $msg = curl_error($curl); curl_close($curl); return false; } curl_close($curl); // Retrieve IP data from API response $ipJsonInfo = json_decode($returnData , true); // Return geolocation data return !empty($ipJsonInfo)?$ipJsonInfo:false; } ?> <!DOCTYPE html> <html lang="en-US"> <head> <title>Get Geolocation from IP Address using PHP by CodeAT21</title> <link rel="stylesheet" href="style.css"> <meta charset="utf-8"> </head> <body> <div class="wrapper"> <div class="container"> <div class="form"> <form method="post" > <label> IP Address: </label> <input type="text" name="ip_address" value="<?php echo !empty($userIP)?$userIP:$_SERVER['REMOTE_ADDR']; ?>" required> <input type="submit" name="submit" value="Get Geolocation Data" class="btn"> </form> <?php if(!empty($statusMsg)){ ?> <h2><?php echo $statusMsg; ?></h2> <?php if(!empty($ipJsonInfo)){ $country_code = $ipJsonInfo['country_code']; $country_name = $ipJsonInfo['country_name']; $region_code = $ipJsonInfo['region_code']; $region_name = $ipJsonInfo['region_name']; $city = $ipJsonInfo['city']; $zip_code = $ipJsonInfo['zip_code']; $latitude = $ipJsonInfo['latitude']; $longitude = $ipJsonInfo['longitude']; $time_zone = $ipJsonInfo['time_zone']; echo '<p><b>Country Name:</b> '.$country_name.'</p>'; echo '<p><b>Country Code:</b> '.$country_code.'</p>'; echo '<p><b>Region Code:</b> '.$region_code.'</p>'; echo '<p><b>Region Name:</b> '.$region_name.'</p>'; echo '<p><b>City:</b> '.$city.'</p>'; echo '<p><b>Zipcode:</b> '.$zip_code.'</p>'; echo '<p><b>Latitude:</b> '.$latitude.'</p>'; echo '<p><b>Longitude:</b> '.$longitude.'</p>'; echo '<p><b>Time Zone:</b> '.$time_zone.'</p>'; }else{ echo '<p>IP data is not found!</p>'; } } ?> </div> </div> </div> </body> </html>
index2.php
<?php // IP address $ip = '162.222.178.77'; $URL = 'https://freegeoip.app/json/'.$ip; // Create a new cURL resource with URL $curl = curl_init($URL); // Return response instead of outputting curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Execute API request $returnData = curl_exec($curl); // Close cURL resource curl_close($curl); // Retrieve IP data from API response $ipJsonInfo = json_decode($returnData , true); ?> <!DOCTYPE html> <html lang="en-US"> <head> <title>Get Geolocation from IP Address using PHP by CodeAT21</title> <link rel="stylesheet" href="style.css"> <meta charset="utf-8"> </head> <body> <div class="wrapper"> <div class="container"> <div class="form"> <h3>Geolocation Information:</h3> <?php if(!empty($ipJsonInfo)){ $country_name = $ipJsonInfo['country_name']; $country_code = $ipJsonInfo['country_code']; $region_code = $ipJsonInfo['region_code']; $region_name = $ipJsonInfo['region_name']; $city = $ipJsonInfo['city']; $zip_code = $ipJsonInfo['zip_code']; $latitude = $ipJsonInfo['latitude']; $longitude = $ipJsonInfo['longitude']; $time_zone = $ipJsonInfo['time_zone']; echo '<p><b>Country Name:</b> '.$country_name.'</p>'; echo '<p><b>Country Code:</b> '.$country_code.'</p>'; echo '<p><b>Region Code:</b> '.$region_code.'</p>'; echo '<p><b>Region Name:</b> '.$region_name.'</p>'; echo '<p><b>City:</b> '.$city.'</p>'; echo '<p><b>Zipcode:</b> '.$zip_code.'</p>'; echo '<p><b>Latitude:</b> '.$latitude.'</p>'; echo '<p><b>Longitude:</b> '.$longitude.'</p>'; echo '<p><b>Time Zone:</b> '.$time_zone.'</p>'; }else{ echo 'IP data is not found!'; } ?> </div> </div> </div> </body> </html>

Download source code from github   |   Live Demo – youtube