Reverse IP Lookup using ipapi with PHP

Website visitors’s IP data is an important part for any website to add user experience. The user’s easily targeted with relevant information such as for advertising, load maps with near by search queries, can display content for their region, display website content in correct language etc.

So if you’re running a website or thinking to build a web application, then you will definitely thinking about to get your website visitors details. You can easily get visitors details using using ipapi REST API.

In this tutorial you will learn how to integrate Ipapi using PHP to get to get IP Address information.

Also, read:

We will cover this tutorial step by step to integrate Ipapi using PHP with example.


Step1: Get Ipapi API Access Key

First we need to create an account on Ipapi to get API Access Key.

We will use API Access key to make HTTP request to get IP Address details.

https://api.ipapi.com/api/161.185.160.93
    ? access_key = YOUR_ACCESS_KEY

Step2: Make HTTP API Request to Ipapi API

We will make HTTP request to ipapi to get IP address details. We will pass API Access Key and IP Address to get details. We can also pass multiple Ip address as comma address. The API will return response JSON data and stored into $responseJsonData variable.

<?php 
$ipAddress = '161.185.160.93';
$access_key = 'YOUR_ACCESS_KEY';

$ch = curl_init('https://api.ipapi.com/'.$ipAddress.'?access_key='.$access_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true

$responseJsonData = curl_exec($ch);
curl_close($ch);
?>

Step3: Display IP Address Details

We will decode API JSON response result data and display IP Address details.

<?php

$result = json_decode($responseJsonData, true);

echo $result['time_zone']['code'];

?>

Step4: Complete Code To Integrate Ipapi API with PHP

Below is the complete code to make HTTP request to Ipapi to get IP address details.


<?php
$ipAddress = '161.185.160.93';
$access_key = 'YOUR_ACCESS_KEY';

$ch = curl_init('https://api.ipapi.com/'.$ipAddress.'?access_key='.$access_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true

$responseJsonData = curl_exec($ch);
curl_close($ch);

$result = json_decode($responseJsonData, true);

echo $result['time_zone']['code'];
?>

Below is the response data in JSON format from Ipapi for the IP Address “161.185.160.93”

{
    "ip": "161.185.160.93",
    "hostname": "161.185.160.93",
    "type": "ipv4",
    "continent_code": "NA",
    "continent_name": "North America",
    "country_code": "US",
    "country_name": "United States",
    "region_code": "NY",
    "region_name": "New York",
    "city": "Brooklyn",
    "zip": "11238",
    "latitude": 40.676,
    "longitude": -73.9629,
    "location": {
        "geoname_id": 5110302,
        "capital": "Washington D.C.",
        "languages": [
            {
                "code": "en",
                "name": "English",
                "native": "English"
            }
        ],
        "country_flag": "http://assets.ipapi.com/flags/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-09-24T05:07:10-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": 22252,
        "isp": "The City of New York"
    },
    "security": {
        "is_proxy": false,
        "proxy_type": null,
        "is_crawler": false,
        "crawler_name": null,
        "crawler_type": null,
        "is_tor": false,
        "threat_level": "low",
        "threat_types": null
    }
}

Step5: Conclusion

In this tutorial you have learned how integrate Ipapi using PHP. You can checkout documentation for advance options and details to integrate API. You can also integrate this API with programming languages other than PHP.

You may also like: