Skip to Content
PHPCODE
get location from ip address php script
php code / September 12, 2021

The Geolocation API enables the developer to extract location data from an IP address and track visitors in a web application. The Geolocation API is highly handy when you want to pinpoint website users and alter functionality accordingly. It offers realtime geolocation data depending on the IP address supplied in the API URL.

There are several Geolocation APIs accessible to obtain geolocation information from an IP address; among them, ipstack is one of the top free Geolocation APIs. The Ipstack API allows you to identify online users in real time based on their IP address. Ipstack offers a robust Geolocation API that searches for and returns accurate location data in JSON or XML format. To obtain geolocation data, the ipstack API can be used in any computer language (PHP, JavaScript, etc.). In this tutorial, we’ll show you how to use PHP to get geolocation from an IP address.

To integrate Geolocation API with ipstack in PHP, follow the simple steps below.

Obtain an API Access Key
A unique authentication key is required to authenticate with the ipstack API. Create your API Access Key before you begin.

Create an ipstack account.
You’ll find the API key in the dashboard’s Your API Access Key section.

get location from ip address php script

Configuring the API
Specify the API Access Key and IP address to receive geolocation data from the ipstack API.

To the API’s base URL, append the IP address.
In the access key parameter, enter the API Access Key.

// 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;

Send an HTTP GET request.
To get the geolocation data, use cURL in PHP to make an HTTP GET request to the Geolocation API.

// 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 perform a secure API request, start the URL with a https and employ HTTPS (SSL) encryption.

https://api.ipstack.com

Geolocation Information
The geolocation data can be returned in JSON or XML format after a successful API request. The ipstack API initially returns the following geolocation data.

IP (Internet Protocol) Address (ip)
Make a selection (type)
(continent code) Continent Code (continent code) Continent Code (continent code) Contin
Name of the Continent (continent name)
(country code) is a country code.
Name of the Country (name of the Country)
(region code) is a code that identifies a certain region.
Name of the Region (region name)
The city (city)
the zip code (zip)
Flexibility (latitude)
Longitude is the measurement of the distance between two points (longitude)
the location (location)
geoname id
capital \slanguages \scountry flag \scountry flag emoji \scountry flag emoji unicode \scalling code \sis eu

To convert a JSON response to an array in PHP, use the json decode() function.

// Convert API json response to array 
$api_result = json_decode($json_resp, true);

Using the ipstack API to get geolocation from an IP address is an example of code.
The following is the complete PHP code for obtaining geolocation from an IP address.

<?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); 
?>

Note :

The ipstack API is free to use, however subscription plans for more sophisticated purposes are available. The most important geolocation data is obtained from the ipstack API with minimal settings in the example code. Additional arguments can be used to tailor the API result and obtain geolocation data that meets your needs. See the ipstack API documentation for a detailed reference.

PHPCODE © 2024