get geolocation in PHP

How to get geolocation in PHP? ( country, zip, latitude, etc.)

In this tutorial, we will learn to get geolocation in PHP for example the Country, Latitude, Longitude, zip code, etc. with the help of IP Addresses.

Geolocation provides information about the visitors’ geographic location. This API uses IP addresses to determine the geographic location of visitors.


So, in order to track all details, we need visitors’ IP addresses. We can easily obtain all geolocation details by using an IP address.

Get the IP Address of the user with PHP

The $_SERVER['REMOTE_ADDR'] gives the IP address from which the request was sent to the web server.

$ipAddress = $_SERVER['REMOTE_ADDR'];

Get geolocation in PHP from the IP address

The following code will generate a response that contains all the information about the geolocation.

// Get the IP address of the user
$ipAddress = $_SERVER['REMOTE_ADDR']; 

//set API URL
$apiURL = 'https://api.ipbase.com/v1/json/' . $ipAddress;

// sample URL
//$apiURL = "https://api.ipbase.com/v1/json/159.203.240.102";

// Make HTTP GET request using cURL
$curl = curl_init($apiURL);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
$response = curl_exec($curl); 
curl_close($curl); 

// get IP details
$ipDetails = json_decode($response, true); 
echo "<pre>";
print_r($ipDetails);
echo "</pre>";

If you print the data it will be something like this. You can easily get the country code, country name, region name, city name, time zone, latitude, longitude, and metro code.

Array
(
    [ip] => 159.203.240.102
    [country_code] => US
    [country_name] => United States
    [region_code] => 
    [region_name] => California
    [city] => San Francisco
    [zip_code] => 94117
    [time_zone] => America/Los_Angeles
    [latitude] => 37.76916885376
    [longitude] => -122.44249725342
    [metro_code] => 0
)

Now you can use the data in your projects.

$countryCode = $ipDetails['country_code']; 
$countryName = $ipDetails['country_name']; 
$regionCode = $ipDetails['region_code']; 
$regionName = $ipDetails['region_name']; 
$city = $ipDetails['city']; 
$zipCode = $ipDetails['zip_code']; 
$latitude = $ipDetails['latitude']; 
$longitude = $ipDetails['longitude']; 
$timeZone = $ipDetails['time_zone']; 

// Print the details
echo 'Country Name: '.$countryName.'<br/>'; 
echo 'Country Code: '.$countryCode.'<br/>'; 
echo 'Region Code: '.$regionCode.'<br/>'; 
echo 'Region Name: '.$regionName.'<br/>'; 
echo 'City: '.$city.'<br/>'; 
echo 'Zipcode: '.$zipCode.'<br/>'; 
echo 'Latitude: '.$latitude.'<br/>'; 
echo 'Longitude: '.$longitude.'<br/>'; 
echo 'Time Zone: '.$timeZone;

Get geolocation details using Net_GeoIP (PEAR GeoIP library)

This package is built to accurately determine the geographic location of an IP address.

  • Net_GeoIP::getInstance(): method to get an instance and avoid re-parsing the database
  • Net_GeoIP::lookupCountryName() : returns a full country name for a specified IP address
  • Net_GeoIP::lookupCountryCode() : returns 2-letter country code (e.g. CA) for specified IP address
  • Net_GeoIP::lookupRegion() : returns the region for the given IP address.
  • Net_GeoIP::lookupLocation() : returns the location record for the specified IP address
  • Net_GeoIP::lookupOrg() : returns the name of the organization or ISP for the given IP address.

Here is an example to get the country name:

require_once "Net/GeoIP.php";

$geoip = Net_GeoIP::getInstance("/path/to/geoipdb.dat");

try {
    echo $geoip->lookupCountryName($_SERVER['REMOTE_ADDR']);
} catch (Exception $e) {
    // Handle exception
}

References

Here is the reference where you can read about the above tutorial in depth.

For software developers, ipbase.com offers a trustworthy & scalable IP Geolocation API. It makes use of a database that has IP addresses linked to cities as well as other pertinent data like time zone, latitudes, and longitude.

learn more about PHP: $_SERVER['REMOTE_ADDR']: https://www.php.net/manual/en/reserved.variables.server.php

PHP cURL details: https://www.php.net/manual/en/ref.curl.php

Add comment