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)
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.
];
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
.
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.