PHP code example of nuthouse-cis / ip-location

1. Go to this page and download the library: Download nuthouse-cis/ip-location 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/ */

    

nuthouse-cis / ip-location example snippets



/** @var $locator \NuthouseCIS\IPLocation\Locator */
$ip = new \NuthouseCIS\IPLocation\Ip('8.8.8.8');
$location = $locator->locate($ip);

if ($location
    && ($location->getCountry()->getIsoAlpha2() === 'US'
        || $location->getCountry()->getIsoAlpha3() === 'USA'
        || $location->getCountry()->getName() === 'United States')
) {
    // Do some stuff
}

/** @var $location \NuthouseCIS\IPLocation\Location\Location */
$decorator = new \NuthouseCIS\IPLocation\Decorators\LocationJsonDecorator($location);
print json_encode($decorator);

/**
 * @var $simpleCacheIterface \Psr\SimpleCache\CacheInterface
 * @var $adapter \NuthouseCIS\IPLocation\Locator
 */
$locator = new \NuthouseCIS\IPLocation\Locators\CacheLocator(
    $adapter,
    $simpleCacheIterface,
    10 * 60,
    'adapter1-'
);

$errorHandler = new class implements \NuthouseCIS\IPLocation\Handlers\ErrorHandler {
    public function handle(Exception $exception): void
    {
        // Do some stuff with exception
    }
};
/**
 * @var $adapter \NuthouseCIS\IPLocation\Locator
 */
$locator = new \NuthouseCIS\IPLocation\Locators\MuteLocator(
    $adapter,
    $errorHandler
);

/**
 * @var $logger \Psr\Log\LoggerInterface
 */
$errorHandler = new \NuthouseCIS\IPLocation\Handlers\PsrLogErrorHandler(
    $logger,
    \Psr\Log\LogLevel::ERROR
);

/**
 * @var $adapters \NuthouseCIS\IPLocation\Locator[]
 */
$locator = new \NuthouseCIS\IPLocation\Locators\ChainLocator(
    ...$adapters
);

/** 
 * @var $client \Psr\Http\Client\ClientInterface
 * @var $requestFactory \Psr\Http\Message\RequestFactoryInterface
 * @var $apiKey string API Key from your dashboard https://app.ipgeolocation.io/
 * @var $lang string [en, de, ru, ja, fr, cn, es, cs, it]
 * @var $fields null|string[] ent,
    $requestFactory,
    $apiKey,
    $lang,
    $fields,
    $excludes,
    $baseUrl
);
$location = $locator->locate(new \NuthouseCIS\IPLocation\Ip('8.8.8.8'));

/**
 * @var $filePath string path to SypexGeo database file
 * @see https://sypexgeo.net/ru/download/
 */
$sxGeo = new \NuthouseCIS\SxGeo\SxGeo(
    $filePath,
    \NuthouseCIS\SxGeo\SxGeo::SXGEO_BATCH | \NuthouseCIS\SxGeo\SxGeo::SXGEO_MEMORY
);
$locator = new \NuthouseCIS\IPLocation\Locators\SypexGeo\SypexGeoAdapter($sxGeo);
$location = $locator->locate(new \NuthouseCIS\IPLocation\Ip('8.8.8.8'));

/** 
 * @var $client \Psr\Http\Client\ClientInterface
 * @var $requestFactory \Psr\Http\Message\RequestFactoryInterface
 * @var $apiKey null|string API Key [Default: null - free plan]
 * @var $server string endpoint domain [Default: api.sypexgeo.net]
 * @see \NuthouseCIS\IPLocation\Locators\SypexGeo\ApiServer
 * @link https://ipgeolocation.io/documentation/ip-geolocation-api.html
 */
$locator = new \NuthouseCIS\IPLocation\Locators\SypexGeo\SypexGeoApiAdapter(
    $client,
    $requestFactory,
    $server,
    $apiKey
);
$location = $locator->locate(new \NuthouseCIS\IPLocation\Ip('8.8.8.8'));

/**
 * @var $filePath string path to IP2Location database file
 * @link https://www.ip2location.com/database/ip2location
 * @link https://lite.ip2location.com/database/ip-country
 */
$db = new \IP2Location\Database($filePath);
/**
 * @var $fields int[] Fields to return
 * @var $

/** 
 * @var $client \Psr\Http\Client\ClientInterface
 * @var $requestFactory \Psr\Http\Message\RequestFactoryInterface
 * @var $apiKey string API Key [Default: null - free plan]
 * @var $lang string 
 * @var $package string Web service package of different granularity of return information.
 * Available values: [WS1, ..., WS24]
 * @var $addons string[]  Extra information in addition to the above selected package.
 * Valid value: continent,country,region,city,geotargeting,country_groupings,time_zone_info
 * @var $baseUrl string Endpoint for API [Default: https://api.ip2location.com/v2/]
 * @link https://ipgeolocation.io/documentation/ip-geolocation-api.html
 */
$locator = new \NuthouseCIS\IPLocation\Locators\Ip2Location\Ip2LocationApiAdapter(
    $client,
    $requestFactory,
    $apiKey,
    $package,
    $lang,
    $addons,
    $baseUrl
);
$location = $locator->locate(new \NuthouseCIS\IPLocation\Ip('8.8.8.8'));