Skip to main content

Build IP Lookup Website using ipapi with PHP

Website visitors details plays an important role for any website to connect with their users to provide best user experience and services. Website owners always looking for its users details, but its not very easy to get complete details of website visitors as they belong from around the world.

So if you’re a website owner or developing a website and wants to get details of your website visitor details, then you’re here at the right place. In this tutorial you will learn how consume Ipapi with PHP to get ip address information.

Also, read:

With ipapi integration in your application, you can easily get your website visitors details using IP Address.

So let’s proceed to consume ipapi with PHP to get IP address details with example.

Step1: Create API Access Key

We first need to sign up to ipapi to create an account to get API Access Key.

We will use created 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 implement functionality to make HTTP request to ipapi to get IP Address details with PHP CURL library. We will use API Access Key and ip address to get IP address details. You can also pass multiple IP address as comma separated to get details.

<?php 
$ip_ddress = '161.185.160.93';
$api_access_key = 'YOUR_ACCESS_KEY';

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

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

Step3: Display IP Address Information

We will use result response JSON data to display IP Address details. We will decode the response JSON data and display results.

<?php

$result = json_decode($response, true);

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

?>

Step4: Complete Code To Consume Ipapi API with PHP

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

<?php
$ip_ddress = '161.185.160.93';
$api_access_key = 'YOUR_ACCESS_KEY';

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

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

$result = json_decode($response, true);

echo $result['location']['languages'];
?>

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 consume Ipapi using PHP. You can also checkout documentation for more options and features to integrate API. You can also integrate API with other programming languages as well.

You may also like: