loading

How to Get Geolocation (Latitude, and Longitude) from IP Address using PHP

How to Get Geolocation (Latitude, and Longitude) from IP Address using PHP

How to Get Geolocation (Latitude, and Longitude) from IP Address using PHP

0 Sales

Free

Geolocation offers information about a user's geographic location. The geolocation service, in particular, uses the IP address to identify the location. The first item needed to monitor the visitor's location is an IP address. We can determine the visitor's geography based on his or her IP address. The PHP $_SERVER variable provides the simplest method for obtaining the user's IP address. Using PHP, you may determine the visitor's location with latitude and longitude based on their IP address. In this lesson, we'll teach you how to use PHP to retrieve a location from an IP address.

 

The Geolocation API provides an easy way to determine a user's location based on their IP address. To get geolocation data from an Internet address, you may utilise a free Geolocation API in PHP. Using PHP, this sample program will utilise the IP Geolocation API to retrieve the location, nation, area, town, longitude, and latitude from an IP address.

 

Get IP Address of User with PHP

 

To retrieve the present user's Internet address in PHP, utilise the REMOTE ADDR index of $_SERVER.

$userIP $_SERVER['REMOTE_ADDR'];

 

Get Location from IP Address using PHP

 

Using PHP, utilise the IP Geolocation API to determine the user's location based on their IP address.

- In PHP, use cURL to make an HTTP GET call to the API.


- Using the json decode() method, convert the API JSON answer to an array.


- Obtain IP information from the API response.

 

In the API response, there is a variety of geolocation information accessible. The following are among the most important location details:

 

- Country Name (country name)


- Country Code (country code)


- Code of Region (region code)


- Name of Region (region name) City (city)


- Zip Code (zip code)


- Latitude (latitude)


- Longitude (longitude)


- Time Zone (time_zone)

 

<?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($chCURLOPT_RETURNTRANSFERtrue); 
 
// Execute API request 
$apiResponse curl_exec($ch); 
 
// Close cURL resource 
curl_close($ch); 
 
// Retrieve IP data from API response 
$ipData json_decode($apiResponsetrue); 
 
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!'; 
} 
 
?>

 

You may group all of the code in a function to improve usability.

 

- The IPtoLocation() method retrieves geolocation information from an IP address.

<?php 
function IPtoLocation($ip){ 
    $apiURL 'https://freegeoip.app/json/'.$ip; 
     
    // Make HTTP GET request using cURL 
    $ch curl_init($apiURL); 
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue); 
    $apiResponse curl_exec($ch); 
    if($apiResponse === FALSE) { 
        $msg curl_error($ch); 
        curl_close($ch); 
        return false; 
    } 
    curl_close($ch); 
     
    // Retrieve IP data from API response 
    $ipData json_decode($apiResponsetrue); 
     
    // Return geolocation data 
    return !empty($ipData)?$ipData:false; 
}

 

In the first parameter, send the IP address to IPtoLocation().

$userIP '162.222.198.75'; 
$locationInfo IPtoLocation($userIP);

 

LICENSE OF USE

You can use it for personal or commercial projects. You can't resell it partially or in this form.

PRODUCT INFO

Create Date : Jan 20, 2022

Updated Date : Jan 23, 2022

Ratings

Comments : 0

Downloads : 0