Skip to content

chrisullyott/php-ip-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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();