Fetch geolocation data for IP addresses from ip-api.com.
$ composer require chrisullyott/php-ip-api
$api = new ChrisUllyott\IpApi();
// Set output language and fields (optional)
$api->setLanguage('en');
$api->setFields(['query', 'country', 'city']);
$response = $api->get('91.198.174.192');
print_r($response);
stdClass Object
(
[country] => Netherlands
[city] => Amsterdam
[query] => 91.198.174.192
)
$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
)
)
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();