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.
In order to authenticate with ipstack API, a unique authentication key is required. Before getting started, create your API Access Key.
To get the geolocation data from ipstack API, specify API Access Key and IP address.
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;
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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 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
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.
Use json_decode()
function to convert the JSON response to array in PHP.
// Convert API json response to array
$api_result = json_decode($json_resp, true);
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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$json_resp = curl_exec($ch);
// Close cURL
curl_close($ch);
// Geolocation data
$api_result = json_decode($json_resp, true);
?>
Get Geolocation (Country, Latitude, and Longitude) from IP Address using PHP
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