chrisullyott/php-ip-api

A PHP wrapper for ip-api.com.

v1.0.1 2020-08-16 00:29 UTC

This package is auto-updated.

Last update: 2024-03-16 07:59:58 UTC


README

Latest Stable Version Total Downloads

PHP IP API

Fetch geolocation data for IP addresses from ip-api.com.

Installation

$ composer require chrisullyott/php-ip-api

Instantiation

$api = new ChrisUllyott\IpApi();

// Set output language and fields (optional)
$api->setLanguage('en');
$api->setFields(['query', 'country', 'city']);

Request one

$response = $api->get('91.198.174.192');
print_r($response);
stdClass Object
(
    [country] => Netherlands
    [city] => Amsterdam
    [query] => 91.198.174.192
)

Request many

$ips = [
    '100.142.29.254',
    '100.142.39.218'
];

$response = $api->get($ips);
print_r($response);
Array
(
    [0] => stdClass Object
        (
            [country] => United States
            [city] => Chicago
            [query] => 100.142.29.254
        )

    [1] => stdClass Object
        (
            [country] => United States
            [city] => Chicago
            [query] => 100.142.39.218
        )
)

Request from a file (create CSV)

Using a newline-separated list of IP addresses, a CSV file of response data will be built.

$file = 'ips.txt';

$list = new ChrisUllyott\IpApiList($file);
$list->setFields(['query', 'country', 'city']);
$list->build();