Skip to content
You can use ipwhois.io to filter out bot traffic, customize content based on visitor's location, display full country names, perform bulk IP geolocation, and more.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md
ip-geolocation-api.php

README.md

IP Geolocation API - PHP SDK

Overview

Use the ipwhois.io IP Geolocation API to quickly and simply integrate IP geolocation into your script or website. Save yourself the hassle of setting up local GeoIP libraries and having to remember to regularly update the data. You can use ipwhois.io to filter out bot traffic, customize content based on visitor's location, display full country names, perform bulk IP geolocation, and more.

Quick Start Guide

You can call the API by sending HTTP GET requests to http://free.ipwhois.io/json/{IP}

{IP} can be an IPv4 or IPv6 address, or none to use the current IP address.

Note: Complete documentation to use this SDK is also available at IP Geolocation API PHP SDK - Documentation.

System Requirements

Internet connection is required to run this component.

Basic Usage

Call method get_ipwhois($ip, $format, $lang) passing IP address as parameters (rest of the parameters are optional) and it will return the Geolocation for the passed IP address. To customize the geolocation response, you can pass the other parameters to get_ipwhois() method as described below:

  • $format

    Based on your choice, the ipwhois API can deliver results in:
    • json (default)
    • xml
    • line
  • $lang

    Pass the language parameter to get the geolocation information in a language other than English. By default, it is set to English language.
    ipwhois provides response in the following languages:
    • en - English (default)
    • de - Deutsch (German)
    • es - Español (Spanish)
    • pt-BR - Español - Argentina (Spanish)
    • fr - Français (French)
    • ja - 日本語 (Japanese)
    • zh-CN - 中国 (Chinese)
    • ru - Русский (Russian)

Example

<?php
    $ip = "CLIENT_IP_ADDRESS";
    $location = get_ipwhois($ip);
    $decodedLocation = json_decode($location, true);
    
    echo "<pre>";
    print_r($decodedLocation);
    echo "</pre>";

    function get_ipwhois($ip, $format = "json", $lang = "en") {
        $url = "http://free.ipwhois.io/".$format."/".$ip."?lang=".$lang;
        $cURL = curl_init();

        curl_setopt($cURL, CURLOPT_URL, $url);
        curl_setopt($cURL, CURLOPT_HTTPGET, true);
        curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json'
        ));
        return curl_exec($cURL);
    }
?>
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.