PHP Requires Chilkat v11.0.0+
PHP
Look up an IP address with ipdata
See more Geolocation Examples
Use an ipdata API key to request a public IPv4 or IPv6 address. The key is encoded as the api-key query parameter. The example displays location fields and the optional ASN object using its current nested structure. Data access and quotas depend on the account plan.
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';
$apiKey = 'YOUR_API_KEY';
$url = new StringBuilder();
$url->Append('https://api.ipdata.co/');
$url->Append($http->UrlEncode($ipAddress));
$parameters = new JsonObject();
$parameters->UpdateString('api-key', $apiKey);
$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;
}
$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->JsonTypeOf('ip') != 1) || ($json->StringOfEquals('ip', '', true))) {
echo "The response did not contain the expected IP address.\n";
exit;
}
echo 'IP address: ', $json->StringOf('ip'), "\n";
if (($json->JsonTypeOf('country_name') == 1) && (!$json->StringOfEquals('country_name', '', true))) {
echo 'Country: ', $json->StringOf('country_name'), "\n";
} else {
echo "Country: unavailable\n";
}
if (($json->JsonTypeOf('country_code') == 1) && (!$json->StringOfEquals('country_code', '', true))) {
echo 'Country code: ', $json->StringOf('country_code'), "\n";
} else {
echo "Country code: unavailable\n";
}
if (($json->JsonTypeOf('region') == 1) && (!$json->StringOfEquals('region', '', true))) {
echo 'Region: ', $json->StringOf('region'), "\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('time_zone.name') == 1) && (!$json->StringOfEquals('time_zone.name', '', true))) {
echo 'Time zone: ', $json->StringOf('time_zone.name'), "\n";
} else {
echo "Time zone: unavailable\n";
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (($json->JsonTypeOf('latitude') == 2) && ($json->JsonTypeOf('longitude') == 2)) {
echo 'Coordinates: ', $json->StringOf('latitude'), ', ', $json->StringOf('longitude'), "\n";
} else {
echo "Coordinates: unavailable\n";
}
if (($json->JsonTypeOf('asn.asn') == 1) && (!$json->StringOfEquals('asn.asn', '', true))) {
echo 'ASN: ', $json->StringOf('asn.asn'), "\n";
} else {
echo "ASN: unavailable\n";
}
if (($json->JsonTypeOf('asn.name') == 1) && (!$json->StringOfEquals('asn.name', '', true))) {
echo 'Network organization: ', $json->StringOf('asn.name'), "\n";
} else {
echo "Network organization: unavailable\n";
}