The Geolocation API permits the developer to get the location info from the IP address and track the users in the web application. It returns realtime geolocation data grounded on the IP address stated in the API URL. The Geolocation API is helpful when you want to locate the website visitors and change the functionality consequently.
There are a lot of Geolocation API accessible to get the geolocation information from IP. Therefore, ipstack is one of the best free Geolocation API among them. Firstly, Ipstack API allows you to identify the web user’s by their IP address in realtime. Secondly, Ipstack offers powerful Geolocation API that is looking up location data and returns exact info in JSON or XML format. At last, the ipstack API can be used in any programming language (PHP, JavaScript, etc.) to get the geolocation data. In this tutorial, we will explain to you how to get geolocation from IP address using PHP.
Follow the steps given below to integrate Geolocation API with ipstack in PHP.
Get API Access Key
It would be good if you had a single authentication key to authenticate with ipstack API. Before proceeding, generate your API Access Key.
- Make an account on ipstack.
- In the dashboard, you will get the API key under the Your API Access Key
API Configuration
Specify API Access Key and IP address if you want to get the geolocation data from ipstack API.
- Attach IP address to the API’s base URL.
- Stipulate 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
Call Geolocation API via HTTP GET request using cURL in PHP, to fetch the geolocation data.
// 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);
SEE ALSO: Distance Between Two Addresses using Google Maps API and PHP
HTTPS Encryption:
To make protected API requests use HTTPS (SSL) encryption.
https://api.ipstack.com
Geolocation Data
After a successful API request, Return the geolocation data 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
You have to 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);
SEE ALSO: Add Custom Marker Icons to Google Map Dynamically from the Database with PHP and MySQL
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($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); ?>
Conclusion
Firstly, ipstack API is free to use; there are also premium plans offered for advanced uses. Secondly, in the example code, an essential geolocation data is raised from the ipstack API with minimal configuration. Thirdly, you can use extra parameters to customize the API result and get geolocation data as per your needs.
Also, read our previous blog- Ajax Pagination with Search and Filter in PHP