brainstud / geolocation
A laravel package to get the country and timezone (geolocation) of the request
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.4
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-11-09 19:56:36 UTC
README
Get the country & timezone of the request with the cloudflare country header or fallback to a paid geo-locator API.
$location = Geolocation::getLocation($request); // { countryCode: "NL", timeZone: "Europe/Amsterdam" }
The aim of this package prioritize the cloudflare header to keep costs down. Only if that fails an API-call will be done. By this philosophy only minimal info will be available. (country code and timezone)
Installation
You can install the package via composer:
composer require brainstud/geolocation
You can publish the config file with:
php artisan vendor:publish --tag="geolocation-config"
This is the contents of the published config file:
return [ 'header' => 'CF-IPCountry', // The cloudflare header that contains the country code 'geocoder' => GeoLocator::IpRegistry, // The geocoder to use if the header is empty 'ip-registry' => [ // Credentials for the ip registry API 'base_url' => env('IP_REGISTRY_URL', 'https://api.ipregistry.co'), 'key' => env('IP_REGISTRY_KEY'), ], 'ip-stack' => [ // Credentials for the ip stack API 'base_url' => env('IP_STACK_URL', 'https://api.ipstack.com'), 'key' => env('IP_STACK_KEY'), ], 'cache_ttl' => DateInterval::createFromDateString('2 months'), // Cache TTL for the geocoder services. ];
Supported services
Contributing
You can add a geocoder by creating a class in the src/GeoLocators
folder that implements GeoLocatorContract
.
Then add it to the GeoLocator
enum and GeoLocatorFactory
.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.