PHP code example of gam6itko / ipquery-client

1. Go to this page and download the library: Download gam6itko/ipquery-client 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/ */

    

gam6itko / ipquery-client example snippets


use Gam6itko\IPQuery\IPQueryClient;
use Gam6itko\IPQuery\IPQueryRequestFactory;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;

$psr17 = new Psr17Factory();

$client = new IPQueryClient(
    httpClient: new Psr18Client(),
    // IPQueryRequestFactory prepends the geo-service base URI to the relative path.
    requestFactory: new IPQueryRequestFactory($psr17, $psr17, 'http://localhost:8080'),
);

$result = $client->lookup('8.8.8.8'); // IPv4 or IPv6; throws Gam6itko\IPQuery\LookupException on failure
echo $result['location']['country_code']; // ISO 3166-1 alpha-2 in UPPER case, e.g. "US"

// Caller's own IP as seen by the geo service:
$client->own();   // full metadata (GET /own/all), same shape as lookup()
$client->ownIp(); // just the IP string (GET /own)

try {
    $client->lookup('8.8.8.8');
} catch (\Gam6itko\IPQuery\LookupException $e) {
    $status = $e->getStatusCode(); // e.g. 503, or null
}

use Gam6itko\IPQuery\CachedIPQuery;

$cached = new CachedIPQuery($client, $psr16Cache);
$cached->lookup('8.8.8.8');

use Gam6itko\IPQuery\RetryingIPQuery;

$resilient = new RetryingIPQuery($client, maxAttempts: 3, baseDelayMs: 100);

// Compose with the cache — retry the live call, then cache the successful result:
$cached = new CachedIPQuery(new RetryingIPQuery($client), $psr16Cache);

use Gam6itko\IPQuery\IPQueryStub;
use Gam6itko\IPQuery\LookupException;

new IPQueryStub($lookupResult);
new IPQueryStub(new LookupException('geo is down'));