PHP code example of skpassegna / ipregistry-php-sdk

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

    

skpassegna / ipregistry-php-sdk example snippets


use Ipregistry\Sdk\IpRegistryClient;
use Ipregistry\Sdk\Config;

// Replace 'YOUR_API_KEY' with your actual Ipregistry API key.
$config = new Config('YOUR_API_KEY');

// (Optional) Customize base URL, timeout, hostname lookup, and output format:
// $config = new Config(
//     'YOUR_API_KEY', 
//     Ipregistry\Sdk\Constants::EU_BASE_URL, // Use EU base URL
//     10,                                   // Set timeout to 10 seconds
//     true,                                  // Enable hostname lookup
//     'xml'                                 // Set output format to XML
// );

$client = new IpRegistryClient($config);

    $ipLookup = new IpLookup($client);
    $response = $ipLookup->lookup('8.8.8.8');

    echo "IP Address: " . $response->getIp() . PHP_EOL;
    echo "Country: " . $response->getCountryName() . PHP_EOL;
    echo "City: " . $response->getCity() . PHP_EOL;
    // ... Access other IP-related data fields
    

    $ips = ['8.8.8.8', '1.1.1.1', '2001:4860:4860::8888'];
    $batchResponse = $ipLookup->batchLookup($ips);

    foreach ($batchResponse->getData()['results'] as $result) {
        echo "IP: " . $result['ip'] . ", Country: " . $result['location']['country']['name'] . PHP_EOL;
    }
    

    $originResponse = $ipLookup->originLookup();

    echo "Originating IP: " . $originResponse->getIp() . PHP_EOL;
    

    $asnLookup = new ASNLookup($client);
    $response = $asnLookup->lookup(15169);

    echo "ASN Name: " . $response->getAsnName() . PHP_EOL;
    // ... Access other ASN-related data fields
    

    $asns = [15169, 20940];
    $batchResponse = $asnLookup->batchLookup($asns);

    foreach ($batchResponse->getData()['results'] as $result) {
        echo "ASN: " . $result['asn'] . ", Name: " . $result['name'] . PHP_EOL;
    }
    

    $originResponse = $asnLookup->originLookup();

    echo "Originating ASN: " . $originResponse->getAsn() . PHP_EOL;
    

    $userAgentParsing = new UserAgentParsing($client);
    $response = $userAgentParsing->parse('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36');

    echo "Browser: " . $response->getUserAgentName() . PHP_EOL;
    echo "Operating System: " . $response->getOsName() . PHP_EOL;
    // ... Access other User-Agent related data fields
    

    $userAgents = [
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
        'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
    ];
    $batchResponse = $userAgentParsing->batchParse($userAgents);

    foreach ($batchResponse->getData()['results'] as $result) {
        echo "User-Agent: " . $result['user_agent']['header'] . ", Browser: " . $result['user_agent']['name'] . PHP_EOL;
    }
    

    $originResponse = $userAgentParsing->originParse();

    echo "Originating User-Agent: " . $originResponse->getUserAgentHeader() . PHP_EOL;
    

$response = $ipLookup->lookup('8.8.8.8');

// Access data using getData()
$data = $response->getData();
echo "Country Code: " . $data['location']['country']['code'] . PHP_EOL; // Assuming JSON format

// Access data using convenience methods
echo "Country Name: " . $response->getCountryName() . PHP_EOL;
echo "Is in EU: " . ($response->isInEu() ? 'Yes' : 'No') . PHP_EOL;

try {
    $response = $ipLookup->lookup('invalid-ip');
} catch (Ipregistry\Sdk\Exception\InvalidIpAddressException $e) {
    echo "Error: " . $e->getMessage() . PHP_EOL;
    echo "Error Code: " . $e->getErrorCode() . PHP_EOL;
    echo "Resolution: " . $e->getResolution() . PHP_EOL; 
} catch (Exception $e) {
    // Handle other exceptions.
}

$config = new Config(
    'YOUR_API_KEY', 
    Ipregistry\Sdk\Constants::EU_BASE_URL, // Use EU base URL
    10,                                   // Set timeout to 10 seconds
    true,                                  // Enable hostname lookup
    'xml'                                 // Set output format to XML
);
bash
composer