ipwhois-io/ip-geolocation-api

A Simple php wrapper for https://ipwhois.io

dev-master 2022-04-26 11:34 UTC

This package is auto-updated.

Last update: 2024-04-26 15:47:47 UTC


README

Overview

This IP Geolocation API service is designed for quick and easy visitors IP geolocation integration into your script or website. Get rid of setting up local GeoIP libraries and forget about regular updates. Our neural network analyzes dozens of sources and almost real-time updates the database.

Quick Start Guide

You can call the API by sending HTTP GET requests to http://ipwho.is/{IP}

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

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

System Requirements

Internet connection is required to run this component.

Basic Usage

Call method get_ipwhois($ip, $format, $lang, $apiKey) 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 = "8.8.4.4"; // CLIENT IPADDRESS
    $apiKey = ""; // Leave blank for free endpoint
    $location = get_ipwhois($ip,'json','en',$apiKey);
    
    echo "<pre>";
    print_r($location);
    echo "</pre>";

    function get_ipwhois($ip, $format = "json", $lang = "en", $apiKey = "") {
        $url = "http://".($apiKey != '' ? 'ipwhois.pro' : 'ipwho.is')."/".$ip."?lang=".$lang.($apiKey != '' ? '&key='.$apiKey : '')."&output=".$format;
        $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 json_decode(curl_exec($cURL),true);
    }
?>