PHP code example of ipgeolocation / ipgeolocation-php-sdk
1. Go to this page and download the library: Download ipgeolocation/ipgeolocation-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ipgeolocation / ipgeolocation-php-sdk example snippets
pgeolocation\Sdk\IpGeolocationClient;
pgeolocation\Sdk\IpGeolocationClient;
$client = new IpGeolocationClient([
'api_key' => getenv('IPGEO_API_KEY'),
]);
try {
$response = $client->lookupIpGeolocation([
'ip' => '8.8.8.8',
]);
echo $response->data->ip . PHP_EOL; // 8.8.8.8
echo ($response->data->location?->country_name ?? '') . PHP_EOL; // United States
echo ($response->data->location?->city ?? '') . PHP_EOL;
echo ($response->data->time_zone?->name ?? '') . PHP_EOL;
echo ($response->metadata->credits_charged ?? 0) . PHP_EOL;
} finally {
$client->close();
}
$client = new IpGeolocationClient([
'api_key' => getenv('IPGEO_API_KEY'),
]);
$client = new IpGeolocationClient([
'request_origin' => 'https://app.example.com',
]);
$client = new IpGeolocationClient([
'api_key' => getenv('IPGEO_API_KEY'),
]);
$response = $client->lookupIpGeolocation();
echo $response->data->ip . PHP_EOL;
$response = $client->lookupIpGeolocation([
'ip' => 'ipgeolocation.io',
]);
echo $response->data->ip . PHP_EOL;
echo ($response->data->domain ?? '') . PHP_EOL; // ipgeolocation.io
echo ($response->data->location?->country_name ?? '') . PHP_EOL;
$response = $client->lookupIpGeolocation([
'ip' => '9.9.9.9',
'cho ($response->data->abuse?->emails[0] ?? '') . PHP_EOL;
$visitorUa = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9';
$response = $client->lookupIpGeolocation([
'ip' => '115.240.90.163',
'
$response = $client->lookupIpGeolocation([
'ip' => '8.8.8.8',
'curity.is_vpn'],
'excludes' => ['currency'],
]);
echo ($response->data->location?->country_name ?? '') . PHP_EOL;
echo ($response->data->security?->threat_score ?? 0) . PHP_EOL;
echo ($response->data->security?->is_vpn ? 'true' : 'false') . PHP_EOL;
$response = $client->lookupIpGeolocationRaw([
'ip' => '8.8.8.8',
'output' => 'xml',
]);
echo $response->data . PHP_EOL;
$response = $client->bulkLookupIpGeolocation([
'ips' => ['8.8.8.8', 'invalid-ip', '1.1.1.1'],
' . PHP_EOL;
echo ($result->data->security?->threat_score ?? 0) . PHP_EOL;
continue;
}
echo ($result->error->message ?? '') . PHP_EOL;
}
$metadata = $response->metadata;
echo $metadata->status_code . PHP_EOL;
echo $metadata->duration_ms . PHP_EOL;
print_r($metadata->headerValues('X-Credits-Charged'));
echo ($metadata->firstHeaderValue('Content-Type') ?? '') . PHP_EOL;
$array = $response->data->toArray();
$json = json_encode($response->data, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
print_r($array);
echo $json . PHP_EOL;
try {
$client->lookupIpGeolocation(['ip' => '8.8.8.8']);
} catch (\Ipgeolocation\Sdk\ApiException $error) {
echo $error->status_code . PHP_EOL;
echo ($error->api_message ?? '') . PHP_EOL;
echo $error->getMessage() . PHP_EOL;
}
bash
composer