Get Country, City, latitude, longitude from IP address in PHP

Country name region name city name latitude and longitude from ip address in PHP; In this tutorial, i am going to show you how do you get user geolocation like country name region name city name latitude and longitude from ip address and display in PHP page.

Note that, You can use a free Geolocation API in PHP to fetch location information from an IP address. This tutorial script will use IP Geolocation API to get location, country, region, city, latitude, and longitude from IP address using PHP.

If you follow this tutorial, then you just have to add the user’s IP address in the PHP script below. And when you enter the user’s ip address, after that you can get the country name region name city name latitude and longitude using free geolocation api.

Get Country Name, Region Name, City Name, latitude & longitude from IP address in PHP

  • Step 1 – Start Apache Web Server
  • Step 2 – Create PHP Project
  • Step 3 – Get Geolocation From IP Address
  • Step 4 – Test This PHP App

Step 1 – Start Apache Web Server

Now, you need to start your apache web server. And as well as start Apache and mysql web server. You can see in the image below how to start apache and mysql server:

start xampp apache mysql server
start xampp apache mysql server

Step 2 – Create PHP Project

In step 2, navigate to your xampp/htdocs/ directory. And inside xampp/htdocs/ directory, create one folder. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 3 – Get Geolocation From IP Address

In step 3, create one file that named index.php file. This php file code will get user geolocation like country name region name city name latitude and longitude from ip address and display in PHP page; So add the following code into your index.php file:

<?php 
 
// IP address 
$userIP = '162.222.198.75'; 
 
// API end URL 
$apiURL = 'https://freegeoip.app/json/'.$userIP; 
 
// Create a new cURL resource with URL 
$ch = curl_init($apiURL); 
 
// Return response instead of outputting 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 
// Execute API request 
$apiResponse = curl_exec($ch); 
 
// Close cURL resource 
curl_close($ch); 
 
// Retrieve IP data from API response 
$ipData = json_decode($apiResponse, true); 
 
if(!empty($ipData)){ 
    $country_code = $ipData['country_code']; 
    $country_name = $ipData['country_name']; 
    $region_code = $ipData['region_code']; 
    $region_name = $ipData['region_name']; 
    $city = $ipData['city']; 
    $zip_code = $ipData['zip_code']; 
    $latitude = $ipData['latitude']; 
    $longitude = $ipData['longitude']; 
    $time_zone = $ipData['time_zone']; 
     
    echo 'Country Name: '.$country_name.'<br/>'; 
    echo 'Country Code: '.$country_code.'<br/>'; 
    echo 'Region Code: '.$region_code.'<br/>'; 
    echo 'Region Name: '.$region_name.'<br/>'; 
    echo 'City: '.$city.'<br/>'; 
    echo 'Zipcode: '.$zip_code.'<br/>'; 
    echo 'Latitude: '.$latitude.'<br/>'; 
    echo 'Longitude: '.$longitude.'<br/>'; 
    echo 'Time Zone: '.$time_zone; 
}else{ 
    echo 'IP data is not found!'; 
} 
 
?>

Step 4 – TestThis PHP App

Finally, you need to open your browser and type http://localhost/demo/ this run project on local machine.

Conclusion

Country name region name city name latitude and longitude from ip address in PHP; Through this tutorial, you have learned how to get user geolocation like country name region name city name latitude and longitude from ip address and display in PHP page.

Recommended PHP Tutorials

Leave a Comment