middlewares / geolocation
Middleware to geolocate the client using the ip address
Installs: 7 821
Dependents: 3
Suggesters: 0
Security: 0
Stars: 12
Watchers: 2
Forks: 2
Open Issues: 0
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]
Middleware to geolocate the client using the ip address and Geocoder and save the result as a request attribute.
Requirements
- PHP >= 7.0
- A PSR-7 http library
- A PSR-15 middleware dispatcher
- A PHP-HTTP adapter, for example Guzzle6
- A Geocoder ip-based provider
Installation
This package is installable and autoloadable via Composer as middlewares/geolocation.
composer require middlewares/geolocation
Example
$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());
Options
__construct(Geocoder\Provider\Provider $provider)
The geocoder provider used to geolocate the client.
It's also recommended to configure it to caching responses.
ipAttribute(string $ipAttribute)
By default uses the REMOTE_ADDR
server parameter to get the client ip. This option allows to use a request attribute. Useful to combine with a ip detection middleware, for example client-ip.
attribute(string $attribute)
The attribute name used to store the client addresses in the server request. By default is client-location
.
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.