Skip to main content

Get Website Visitors Info using IPStack API with PHP

Gathering information of your website visitors is not an easy task when you have decent website traffic. The website visitors information is important for your business as it helps you to target your customer in a right way with details.

In this scenario you definitely looking for solutions to get your website visitors information. Don’t worry, it’s very easy. You can use ipstack API to get website visitor details using IP Address. The API offers a powerful, real-time IP Address to Geolocation API to get details quickly into JSON or XML format. The API result data includes Visitors locations,
currency, country and much more.

So in this tutorial, you will learn how to integrate ipstack API using PHP to get IP Address details.

Also, read:

We will cover this tutorial step by step to integrate ipstack API using PHP with example. So let’s proceed.

Step1: Get ipstack API Access Key

For accessing ipstack API, we need to register ipstack API and to get API Access Key. We will use that API Access key to get the IP Address details.

We will need to pass IP Address and Access Key to API URL to get details.

https://api.ipstack.com/155.52.187.7?access_key=YOUR_API_ACCESS_KEY

Step2: Make HTTP Request to IPStack API

We will make HTTP request to ipstack API with details such as IP Address and API Access Key to get the IP Address data. We will use Curl module from PHP to make HTTP request to ipstack API URL https://api.ipstack.com. The API returns result response data into JSON format and store into $response_data variable.

<?php 
$ip_address = '155.52.187.7';
$api_access_key = 'YOUR_API_ACCESS_KEY';
$ch = curl_init('https://api.ipstack.com/'.$ip_address.'?access_key='.$api_access_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_data = curl_exec($ch);
curl_close($ch);
?>

Step3: Decode Response Data and Display Result

We will decode the response JSON data using PHP function json_decode and display result using $result_data JSON data.

<?php 
$result_data = json_decode($response_data, true);
echo $result_data['location']['capital'];
?>

Step4: Complete Code To Get IP Address Details with ipstack API

Below is the complete code to get Website Visitor’s IP Address details using ipstack API with PHP Curl by making HTTP request.

<?php 
$ip_address = '155.52.187.7';
$api_access_key = 'YOUR_API_ACCESS_KEY';
$ch = curl_init('https://api.ipstack.com/'.$ip_address.'?access_key='.$api_access_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_data = curl_exec($ch);
curl_close($ch);
$result_data = json_decode($resultData, true);
echo $result_data['location']['capital'];
?>

Below is the response data in JSON format from ipstack API for the IP Address 155.52.187.7

{
  "ip": "155.52.187.7",
  "type": "ipv4",
  "continent_code": "NA",
  "continent_name": "North America",
  "country_code": "US",
  "country_name": "United States",
  "region_code": "MA",
  "region_name": "Massachusetts",
  "city": "Boston",
  "zip": "02115",
  "latitude": 42.3424,
  "longitude": -71.0878,
  "location": {
    "geoname_id": 4930956,
    "capital": "Washington D.C.",
    "languages": [
        {
          "code": "en",
          "name": "English",
          "native": "English"
        }
    ],
    "country_flag": "https://assets.ipstack.com/images/assets/flags_svg/us.svg",
    "country_flag_emoji": "πŸ‡ΊπŸ‡Έ",
    "country_flag_emoji_unicode": "U+1F1FA U+1F1F8",
    "calling_code": "1",
    "is_eu": false
  },
  "time_zone": {
    "id": "America/New_York",
    "current_time": "2018-03-30T07:54:25-04:00",
    "gmt_offset": -14400,
    "code": "EDT",
    "is_daylight_saving": true
  },
  "currency": {
    "code": "USD",
    "name": "US Dollar",
    "plural": "US dollars",
    "symbol": "$",
    "symbol_native": "$"
  },
  "connection": {
    "asn": 40127,
    "isp": "Longwood Medical and Academic Area (LMA)"
  }
}

Step5: Conclusion

In this tutorial we have explained how to integrate ipstack API to get website visitors IP address information with PHP. You can also gone through documentation for more advance options and feature to further integrate into your application to know your website visitors details. You can integrate the ipstack API using your desired programming languages.

You may also like:

One thought on “Get Website Visitors Info using IPStack API with PHP

  1. Step 4: (line 8)

    ‘$result_data = json_decode($resultData, true);’

    appears to be incorrect. It should read the same as Step 3: (line 2)

    ‘$result_data = json_decode($response_data, true);’

Comments are closed.