PHP code example of ipregistry / ipregistry-php

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

    

ipregistry / ipregistry-php example snippets




use Ipregistry\Exception\IpregistryException;
use Ipregistry\IpregistryClient;

$client = new IpregistryClient('YOUR_API_KEY');

// Look up data for a given IPv4 or IPv6 address.
// On the server side, retrieve the client IP from the request headers.
try {
    $info = $client->lookup('54.85.132.205');
    echo $info->location->country->name;
} catch (IpregistryException $e) {
    // Handle API or network failures (see "Errors" below).
}

$origin = $client->lookupOrigin();
echo $origin->ip, ' ', $origin->location->country->name;

$list = $client->lookupBatch(['73.2.2.2', '8.8.8.8', '2001:67c:2e8:22::c100:68b']);

foreach ($list as $result) {
    if ($result->error !== null) {
        // Handle a per-entry error (e.g. an invalid IP address).
        echo 'entry failed: ', $result->error->getMessage(), PHP_EOL;
        continue;
    }
    echo $result->info->location->country->name, PHP_EOL;
}

$client = new IpregistryClient('YOUR_API_KEY', maxBatchSize: 256);

$info = $client->lookup('8.8.8.8',
    hostname: true,                              // resolve reverse-DNS hostname
    fields: 'location.country.name,security',    // select only these fields
);

use Ipregistry\Cache\InMemoryCache;
use Ipregistry\IpregistryClient;

$client = new IpregistryClient('YOUR_API_KEY',
    cache: new InMemoryCache(maxSize: 8192, defaultTtl: 600),
);

use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Psr16Cache;

$cache = new Psr16Cache(new RedisAdapter($redisConnection));
$client = new IpregistryClient('YOUR_API_KEY', cache: $cache, cacheTtl: 600);

$client = new IpregistryClient('YOUR_API_KEY',
    maxRetries: 3,                  // 0 disables retries entirely
    retryInterval: 1.0,             // base backoff in seconds (interval * 2^attempt)
    retryOnServerError: true,       // retry on 5xx (default: true)
    retryOnTooManyRequests: true,   // retry on 429 (default: false)
);

$client = new IpregistryClient('YOUR_API_KEY', timeout: 5.0);

use Ipregistry\Http\Psr18Transport;

$client = new IpregistryClient('YOUR_API_KEY',
    transport: new Psr18Transport($psr18Client, $requestFactory, $streamFactory),
);

use Ipregistry\Enum\ErrorCode;
use Ipregistry\Exception\ApiException;
use Ipregistry\Exception\ClientException;

try {
    $info = $client->lookup('8.8.8.8');
} catch (ApiException $e) {
    if ($e->errorCode === ErrorCode::InsufficientCredits) {
        // handle exhausted credits
    } elseif ($e->errorCode === ErrorCode::TooManyRequests) {
        // handle rate limiting
    }
} catch (ClientException $e) {
    // handle network / decoding error
}

$list = $client->parseUserAgents('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/120.0');

$userAgent = $list->at(0);
echo $userAgent->name, ' on ', $userAgent->operatingSystem->name;

use Ipregistry\UserAgents;

if (!UserAgents::isBot($_SERVER['HTTP_USER_AGENT'] ?? '')) {
    $info = $client->lookup($clientIp);
    // ...
}
bash
composer 
bash
IPREGISTRY_API_KEY=YOUR_API_KEY php examples/single.php