Geolocation from IP Address using PHP

The Geolocation API allows the developer to get the location information from the IP address and track the visitors in the web application. It returns realtime geolocation data based on the IP address specified in the API URL. The Geolocation API is very useful when you want to locate the website visitors and adjust the functionality accordingly.

There are various Geolocation API available to get the geolocation info from IP, ipstack is one of the best free Geolocation API among them. Ipstack API enables you to identify the web user’s by their IP address in realtime. Ipstack provides powerful Geolocation API that looking up location data and returns accurate info in JSON or XML format. The ipstack API can be used in any programming language (PHP, JavaScript, etc.) to get the geolocation data. In this tutorial, we will show you how to get geolocation from IP address using PHP.

Follow the below simple steps to integrate Geolocation API with ipstack in PHP.

Get API Access Key

In order to authenticate with ipstack API, a unique authentication key is required. Before getting started, create your API Access Key.

  • Create an account on ipstack.
  • In the dashboard, you will get the API key under the Your API Access Key.
    geolocation-api-access-key-codexworld

API Configuration

To get the geolocation data from ipstack API, specify API Access Key and IP address.

  • Append IP address to the API’s base URL.
  • Specify the API Access Key in the access_key parameter.
// Set IP address and API access key  
$ip '38.132.116.186';
$access_key 'YOUR_ACCESS_KEY';

// API URL
$apiURL 'http://api.ipstack.com/'.$ip.'?access_key='.$access_key;

Make HTTP GET Request

To fetch the geolocation data, call Geolocation API via HTTP GET request using cURL in PHP.

// API URL 
$apiURL 'http://api.ipstack.com/'.$ip.'?access_key='.$access_key;

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$json_resp curl_exec($ch);

// Close cURL
curl_close($ch);

HTTPS Encryption:
To make secure API request use HTTPS (SSL) encryption by calling API URL begins with an https.

https://api.ipstack.com

Geolocation Data

After a successful API request, the geolocation data can be returned in JSON or XML format. Initially, the ipstack API returns the following geolocation data.

  • IP Address (ip)
  • Type (type)
  • Continent Code (continent_code)
  • Continent Name (continent_name)
  • Country Code (country_code)
  • Country Name (country_name)
  • Region Code (region_code)
  • Region Name (region_name)
  • City (city)
  • Zipcode (zip)
  • Latitude (latitude)
  • Longitude (longitude)
  • Location (location)
    • geoname_id
    • capital
    • languages
    • country_flag
    • country_flag_emoji
    • country_flag_emoji_unicode
    • calling_code
    • is_eu

Use json_decode() function to convert the JSON response to array in PHP.

// Convert API json response to array 
$api_result json_decode($json_resptrue);

Example Code to Get Geolocation from IP via ipstack API

The following are the complete code to get geolocation from IP address using PHP.

<?php 

// Set IP address and API access key 
$ip '38.132.116.186';
$access_key 'YOUR_ACCESS_KEY';

// API URL
$apiURL 'http://api.ipstack.com/'.$ip.'?access_key='.$access_key;

// Initialize cURL
$ch curl_init();

// Set URL and other appropriate options
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$json_resp curl_exec($ch);

// Close cURL
curl_close($ch);

// Geolocation data
$api_result json_decode($json_resptrue);

?>

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

Conclusion

The ipstack API is free to use, there also premium plans are available for advanced uses. In the example code, the most required geolocation data is fetched from the ipstack API with minimal configuration. You can use additional parameters to customize the API result and get geolocation data as per your needs. For a complete reference, see the documentation of ipstack API.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up