PHP Requires Chilkat v11.0.0+
PHP
Look up an IP address with the ip-api free endpoint
See more Geolocation Examples
This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.
Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
$ipAddress = '8.8.8.8';
$http = new Http();
$http->RequireSslCertVerify = true;
$http->ConnectTimeout = 10;
$http->ReadTimeout = 30;
$http->Accept = 'application/json';
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
$url = new StringBuilder();
$url->Append('http://ip-api.com/json/');
$url->Append($http->UrlEncode($ipAddress));
$parameters = new JsonObject();
$parameters->UpdateString('fields', 'status,message,query,country,countryCode,regionName,city,lat,lon,timezone');
$response = new HttpResponse();
try {
$http->HttpParams('GET', $url->GetAsString(), $parameters, $response);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($response->StatusCode == 0) {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
echo "No HTTP response. Check connectivity, DNS, and TLS configuration.\n";
exit;
}
// Save these values if the application will make further requests.
$remaining = $response->GetHeaderField('X-Rl');
if ($remaining === '0') {
echo 'Rate limit reached. Wait ', $response->GetHeaderField('X-Ttl'), " seconds before the next request.\n";
}
$json = new JsonObject();
try {
$json->Load($response->BodyStr);
$parsed = true;
} catch (\Chilkat2\Exception $e) {
$parsed = false;
}
if ((!$success) || (($response->StatusCode < 200) || ($response->StatusCode >= 300))) {
echo 'Lookup failed. HTTP ', $response->StatusCode, "\n";
if ($parsed) {
if ($json->JsonTypeOf('message') == 1) {
echo $json->StringOf('message'), "\n";
}
}
exit;
}
if (!$parsed) {
echo "Expected a JSON object in the lookup response.\n";
exit;
}
if (!$json->StringOfEquals('status', 'success', true)) {
echo "The service could not complete the lookup.\n";
if ($json->JsonTypeOf('message') == 1) {
echo $json->StringOf('message'), "\n";
}
exit;
}
if (($json->JsonTypeOf('query') != 1) || ($json->StringOfEquals('query', '', true))) {
echo "The response did not contain the expected IP address.\n";
exit;
}
echo 'IP address: ', $json->StringOf('query'), "\n";
if (($json->JsonTypeOf('country') == 1) && (!$json->StringOfEquals('country', '', true))) {
echo 'Country: ', $json->StringOf('country'), "\n";
} else {
echo "Country: unavailable\n";
}
if (($json->JsonTypeOf('countryCode') == 1) && (!$json->StringOfEquals('countryCode', '', true))) {
echo 'Country code: ', $json->StringOf('countryCode'), "\n";
} else {
echo "Country code: unavailable\n";
}
if (($json->JsonTypeOf('regionName') == 1) && (!$json->StringOfEquals('regionName', '', true))) {
echo 'Region: ', $json->StringOf('regionName'), "\n";
} else {
echo "Region: unavailable\n";
}
if (($json->JsonTypeOf('city') == 1) && (!$json->StringOfEquals('city', '', true))) {
echo 'City: ', $json->StringOf('city'), "\n";
} else {
echo "City: unavailable\n";
}
if (($json->JsonTypeOf('timezone') == 1) && (!$json->StringOfEquals('timezone', '', true))) {
echo 'Time zone: ', $json->StringOf('timezone'), "\n";
} else {
echo "Time zone: unavailable\n";
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (($json->JsonTypeOf('lat') == 2) && ($json->JsonTypeOf('lon') == 2)) {
echo 'Coordinates: ', $json->StringOf('lat'), ', ', $json->StringOf('lon'), "\n";
} else {
echo "Coordinates: unavailable\n";
}