AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

Geolocation from IP Address using PHP

Get API Access Key

Lakshika Mathur by Lakshika Mathur
December 20, 2019
Reading Time: 4 mins read
0
Geolocation from IP Address using PHP

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.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

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

Tags: GeolocationIP Address
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
61
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
739
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
162
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
82
How to Connect to the Remote MySQL Database using PHP
PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

January 1, 2020
28
How to Generate QR Code with PHP using Google Chart API
PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

January 1, 2020
83
Next Post
Radius Based Location Search with PHP and MySQL

Radius Based Location Search with PHP and MySQL

Ajax File Upload using jQuery and PHP

Ajax File Upload using jQuery and PHP

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version