nuhouse-cis/ip-location

使用IP地址确定地理位置的第三方服务实现的一组简单示例

0.1.0 2021-04-24 15:56 UTC

This package is auto-updated.

Last update: 2024-09-25 02:37:20 UTC


README

使用IP地址确定地理位置的第三方服务实现的一组简单示例。

此库基于知识库 Deworker.pro 的屏幕录制

安装

安装此扩展的首选方式是通过 composer

composer require nuthouse-cis/ip-location

或者在您的 composer.json 文件中添加

{
  "require": {
    "nuthouse-cis/ip-location": "*"
  }
}

示例

基本用法

require 'vendor/autoload.php';
/** @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
}

也放置了JSON装饰器

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

缓存定位器

此定位器使用PSR-16 Simple Cache接口缓存 \NuthouseCIS\IPLocation\Locator 的另一个实现的返回结果

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

静音定位器

静音定位器捕获所有 异常 并使用 \NuthouseCIS\IPLocation\Handlers\ErrorHandler 的实现来处理它们

$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
);

此外,您还可以使用 \NuthouseCIS\IPLocation\Handlers\PsrLogErrorHandler

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

链定位器

链定位器从所有传递的 \NuthouseCIS\IPLocation\Locator 实现中返回最完整的结果

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

服务

IpGeoLocation

IP Geolocation API 文档

/** 
 * @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[] required fields. Default: ['geo'] - minimum sufficient set of fields
 * @var $excludes null|string[] excluded fields
 * @var $baseUrl string default: https://api.ipgeolocation.io/ipgeo
 * @link https://ipgeolocation.io/documentation/ip-geolocation-api.html
 */
$locator = new \NuthouseCIS\IPLocation\Locators\IpGeoLocationIo\IpGeoLocationIoAdapter(
    $client,
    $requestFactory,
    $apiKey,
    $lang,
    $fields,
    $excludes,
    $baseUrl
);
$location = $locator->locate(new \NuthouseCIS\IPLocation\Ip('8.8.8.8'));

SypexGeo

SypexGeo 文档

数据库文件

/**
 * @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'));

REST API

SypexGeo REST API 文档

/** 
 * @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'));

IP2Location

数据库文件

IP2Location 文档

/**
 * @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 $requiredFields int[] Required fields. Throws exception, if field empty or not supported
 * If null, copy values from $fields param
 */
$locator = new \NuthouseCIS\IPLocation\Locators\Ip2Location\Ip2LocationAdapter(
    $db,
    $fields,
    $requiredFields
);
$location = $locator->locate(new \NuthouseCIS\IPLocation\Ip('8.8.8.8'));

API

IP2Location API 文档

/** 
 * @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'));

测试

# Unit tests:
composer run-script test
# or
$ vendor/bin/phpunit

# Code style:
composer run-script phpcs
# or
$ vendor/bin/phpcs --standard=phpcs.xml

# Static analysis tool (Psalm):
composer run-script psalm
# or
$ vendor/bin/psalm --config=psalm.xml