PHP SDK · Feature

IP Geolocation in PHP

Turn any IP address into geolocation, network, and threat intelligence with one call. The lookup method on the official ip-api-io/ipapi-php client returns the country, city, coordinates, timezone, ISP, and ASN of an IP — plus the suspicious_factors flags used for fraud screening — in a single response.

Trusted by thousands of businesses
Fast JSON API responses
Real-time validation
Simple integration, SDKs & examples
What it does

Geolocation without browser GPS

IP-based geolocation works entirely server-side — no permission prompt, no client SDK, and it works for every visitor and every log line you already have. ip-api.io resolves an IP to a physical location and, uniquely, returns security signals (VPN, proxy, Tor, datacenter, threat) in the same response, so you can localize content and screen for fraud in one round trip.

Prerequisites

  • PHP 8.1 or higher
  • The ip-api-io/ipapi-php package installed (composer require ip-api-io/ipapi-php)
  • A free ip-api.io API key
1

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'];
Behind a proxy? Pass the real client IP from the X-Forwarded-For header to lookup($ip) rather than relying on $_SERVER['REMOTE_ADDR']. See the SDK overview for a full example.
2

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'];
}
Reference

Response reference

lookup() returns an associative array. Location fields are null for private or unrecognized addresses.

FieldTypeDescription
ipstringThe looked-up address
ispstring | nullInternet service provider
asnstring | nullAutonomous system the IP belongs to
locationarraycountry, country_code, city, latitude, longitude, zip, timezone, local_time, local_time_unix, is_daylight_savings
suspicious_factorsarrayis_proxy, is_vpn, is_tor_node, is_datacenter, is_spam, is_crawler, is_threat
FAQ

Frequently asked questions

How do I get the location of an IP address in PHP?

Install ip-api-io/ipapi-php, construct a client with your API key, and call $client->lookup($ip). The returned array has a location array with country, city, latitude, longitude and timezone, plus a suspicious_factors array with VPN/proxy/Tor flags.

Can I geolocate the caller's own IP without passing an address?

Yes. Call $client->lookup() with no argument and the API resolves the IP from the request. Behind a proxy, pass the real client IP from the X-Forwarded-For header to $client->lookup($ip) instead.

How do I look up many IP addresses at once?

Use $client->lookupBatch($ips) with up to 100 addresses in one request. It returns a results array of IP to info plus total_processed, successful_lookups and failed_lookups, and throws if the array is empty or longer than 100.

Is IP geolocation accurate?

Country-level accuracy is about 99.8% and city-level 85–95% depending on region. Coordinates have a median accuracy radius of roughly 50 km (WGS84). It is ideal for routing, localization, timezone detection and fraud signals — not street-level precision.

Start building with the ip-api.io PHP SDK

Run composer require ip-api-io/ipapi-php, drop in your free API key, and ship in minutes — one zero-dependency client for geolocation, fraud detection, and email validation.

Need support?

Explore how IP-API.io can enhance your security, provide robust bot protection, and improve IP geolocation accuracy for your applications.

Contact Support

Found a bug in the SDK?

The ip-api-io/ipapi-php package is open source. Open an issue or pull request on GitHub and we'll take a look.

Open on GitHub