16

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

 3 years ago
source link: https://www.codexworld.com/get-geolocation-country-latitude-longitude-from-ip-address-using-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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



Geolocation provides information about the geographic location of a user. Specifically, the IP address is used by the geolocation service to determine the location. To track the visitor’s location, the first thing needed is an IP address. Based on the IP address, we can collect the geolocation info of the visitor. The PHP $_SERVER variable is the easiest way to get the IP address of the user. Based on the visitor’s IP address, you can detect the location with latitude and longitude using PHP. In this tutorial, we will show you how to get location from IP address using PHP.

The Geolocation API is an instant way to find the location of a user by IP address. You can use a Geolocation API in PHP to fetch location information from an IP address. In this example script, we will use IP Geolocation API to get location, country name, latitude, and longitude from IP address using PHP.

Get IP Address of User with PHP

Use the REMOTE_ADDR index of $_SERVER to get the current user’s IP address in PHP.

$userIP = $_SERVER['REMOTE_ADDR'];

Get Location from IP Address using PHP

Use the IP Geolocation API to get the user’s location from IP using PHP.

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

There are various info is available about geolocation in API response. Some of the most useful location details are:

  • Continent Name (continent)
  • Country Name (name)
  • Country Alpha-2 Code (alpha2)
  • Country Alpha-3 Code (alpha3)
  • Country Numeric Code (country_code)
  • Country International Call Prefix Code (international_prefix)
  • Currency Code (currency_code)
  • Latitude (geo.latitude)
  • Longitude (geo.longitude)
<?php 
// IP address
$userIP = '162.222.198.75';

// API end URL
$apiURL = 'https://api.ipgeolocationapi.com/geolocate/'.$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)){
    $continent = $ipData['continent'];
    $country_code_alpha2 = $ipData['alpha2'];
    $country_code_alpha3 = $ipData['alpha3'];
    $country_name = $ipData['name'];
    $country_code_numeric = $ipData['country_code'];
    $international_prefix = $ipData['international_prefix'];
    $currency_code = $ipData['currency_code'];
    $latitude = $ipData['geo']['latitude'];
    $longitude = $ipData['geo']['longitude'];

echo 'Continent Name: '.$continent.'<br/>';
    echo 'Country Name: '.$country_name.'<br/>';
    echo 'Country Alpha-2 Code: '.$country_code_alpha2.'<br/>';
    echo 'Country Alpha-3 Code: '.$country_code_alpha3.'<br/>';
    echo 'Country Numeric Code: '.$country_code_numeric.'<br/>';
    echo 'Country International Call Prefix Code: '.$international_prefix.'<br/>';
    echo 'Currency Code: '.$currency_code.'<br/>';
    echo 'Latitude: '.$latitude.'<br/>';
    echo 'Longitude: '.$longitude;
}else{
    echo 'IP data is not found!';
}

For better usability, you can group all the code in a function.

  • The following IPtoLocation() function returns geolocation data from IP address.
<?php 
function IPtoLocation($ip){
    $apiURL = 'https://api.ipgeolocationapi.com/geolocate/'.$ip;

// Make HTTP GET request using cURL
    $ch = curl_init($apiURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $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($apiResponse, true);

// Return geolocation data
    return !empty($ipData)?$ipData:false;
}

Call IPtoLocation() and pass the IP address in the first argument.

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

Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request

If you have any questions about this script, submit it to our QA community - Ask Question


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK