https://github.com/brainstudnl/geolocation
Get the geolocation of the request using various methods
https://github.com/brainstudnl/geolocation
Last synced: 18 days ago
JSON representation
Get the geolocation of the request using various methods
- Host: GitHub
- URL: https://github.com/brainstudnl/geolocation
- Owner: brainstudnl
- License: mit
- Created: 2022-04-12T13:21:57.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-09T17:38:30.000Z (over 2 years ago)
- Last Synced: 2025-06-02T19:24:49.135Z (about 1 year ago)
- Language: PHP
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Geolocation
[](https://packagist.org/packages/brainstud/geolocation)
[](https://github.com/brainstud/geolocation/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/brainstud/geolocation/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[](https://packagist.org/packages/brainstud/geolocation)
Get the country & timezone of the request with the cloudflare country header or fallback to a paid geo-locator API.
```php
$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:
```bash
composer require brainstud/geolocation
```
You can publish the config file with:
```bash
php artisan vendor:publish --tag="geolocation-config"
```
This is the contents of the published config file:
```php
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
- [Cloudflare IP geolocation](https://support.cloudflare.com/hc/en-us/articles/200168236-Configuring-Cloudflare-IP-Geolocation)
- [ipregistry](https://ipregistry.co/)
- [ipstack](https://ipstack.com)
## 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](CHANGELOG.md) for more information on what has changed recently.
## Credits
- [Niek Pijp](https://github.com/niekp)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.