middlewares / geolocation
中间件,通过IP地址定位客户端
v3.0.0
2020-12-04 00:07 UTC
Requires
- php: ^7.2 || ^8.0
- php-http/message: ^1.6
- psr/http-server-middleware: ^1.0
- willdurand/geocoder: ^4.0
Requires (Dev)
- eloquent/phony-phpunit: ^6.0|^7.0
- friendsofphp/php-cs-fixer: ^2.0
- geocoder-php/free-geoip-provider: ^4.1
- laminas/laminas-diactoros: ^2.3
- middlewares/utils: ^2.1
- php-http/guzzle6-adapter: ^1.1
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
![SensioLabs Insight][ico-sensiolabs]
通过IP地址和Geocoder定位客户端,并将结果保存为请求属性。
要求
- PHP >= 7.0
- 一个PSR-7 http库
- 一个PSR-15中间件调度器
- 一个PHP-HTTP适配器,例如Guzzle6
- 一个基于IP的Geocoder提供者
安装
此包可通过Composer安装和自动加载,作为middlewares/geolocation
。
composer require middlewares/geolocation
示例
$freeGeoIpProvider = new Geocoder\Provider\FreeGeoIp($adapter); $dispatcher = new Dispatcher([ new Middlewares\Geolocation($freeGeoIpProvider), function ($request) { //Get the client location $location = $request->getAttribute('client-location'); $country = $location->first()->getCountry(); } ]); $response = $dispatcher->dispatch(new ServerRequest());
选项
__construct(Geocoder\Provider\Provider $provider)
用于定位客户端的geocoder提供者。
建议将其配置为缓存响应。
ipAttribute(string $ipAttribute)
默认使用REMOTE_ADDR
服务器参数获取客户端IP。此选项允许使用请求属性。与IP检测中间件结合使用很有用,例如client-ip。
attribute(string $attribute)
用于在服务器请求中存储客户端地址的属性名称。默认为client-location
。
有关最近更改的更多信息,请参阅CHANGELOG,有关贡献的详细信息,请参阅CONTRIBUTING。
MIT许可(MIT)。有关更多信息,请参阅LICENSE。