Prerequisites
- PHP 8.1 or higher
- The
ip-api-io/ipapi-phppackage installed (composer require ip-api-io/ipapi-php) - A free ip-api.io API key
Geolocate one IP with lookup
Pass any IPv4 or IPv6 address, or omit the argument to geolocate the caller's own IP.
<?php
require 'vendor/autoload.php';
use IpApiIo\Client;
$client = new Client(apiKey: getenv('IP_API_IO_KEY'));
$info = $client->lookup('8.8.8.8');
echo $info['ip']; // "8.8.8.8"
echo $info['isp']; // "Google LLC"
echo $info['location']['country']; // "United States"
echo $info['location']['city']; // "Mountain View"
echo $info['location']['timezone']; // "America/Los_Angeles"
var_dump($info['suspicious_factors']['is_datacenter']); // true
Resolve the IP of the machine making the request by calling lookup() with
no argument:
$me = $client->lookup();
echo $me['ip'], ' ', $me['location']['country']; X-Forwarded-For header to lookup($ip) rather than relying on
$_SERVER['REMOTE_ADDR']. See the
SDK overview for a full example.
Geolocate up to 100 IPs with lookupBatch
Enrich logs, sign-up events, or historical data without a round trip per IP. The SDK
validates the limit client-side and throws an InvalidArgumentException if
the array is empty or longer than 100.
$batch = $client->lookupBatch(['8.8.8.8', '1.1.1.1', '9.9.9.9']);
echo $batch['total_processed']; // 3
echo $batch['successful_lookups']; // 3
echo $batch['failed_lookups']; // 0
foreach ($batch['results'] as $ip => $info) {
echo $ip, ' ', $info['location']['country'], ' ', $info['suspicious_factors']['is_vpn'];
}