Skip to content

A Laravel package to get IP addresses geographical location.

License

Notifications You must be signed in to change notification settings

reefki/laravel-geoip

Repository files navigation

Laravel GeoIP

tests

A Laravel package to get geographical location information from IP addresses.

Requirements

  • PHP 8.1 or higher
  • Laravel 10, 11, or 12

Installation

Install the package via Composer:

composer require reefki/laravel-geoip

Optionally, publish the config file:

php artisan vendor:publish --provider="Reefki\Geoip\GeoipServiceProvider" --tag="config"

Usage

Using the Facade

use Reefki\Geoip\Geoip;

$geoip = Geoip::get('8.8.8.8');

$geoip->driver;         // geojs
$geoip->ip;             // 8.8.8.8
$geoip->city;           // Mountain View
$geoip->region;         // California
$geoip->country;        // United States
$geoip->country_code;   // US
$geoip->continent_code; // NA
$geoip->timezone;       // America/Los_Angeles
$geoip->latitude;       // 37.751
$geoip->longitude;      // -97.822
$geoip->cached;         // true or false

IPv6 addresses are also supported:

$geoip = Geoip::get('2001:4860:4860::8888');

Disabling Cache

By default, results are cached. To get realtime data:

Geoip::get('8.8.8.8', cache: false);

Using with Request

Get GeoIP data for the current request's IP address:

$geoip = $request->geoip();

With anonymized IP (for GDPR compliance):

$geoip = $request->geoip(anonymize: true);

Get just the anonymized IP address:

$anonymizedIp = $request->anonymizedIp();
// 8.8.8.8 → 8.8.8.0
// 2001:4860:4860::8888 → 2001:4860:4860::

Configuration

Default Driver

Set the default driver in your .env file:

GEOIP_DEFAULT_DRIVER=geojs

Cache Settings

Configure caching behavior:

GEOIP_CACHE_STORE=redis
GEOIP_CACHE_TTL=86400

HTTP Settings

Configure timeout and retry for API requests:

GEOIP_TIMEOUT=10
GEOIP_RETRY=3

Drivers

GeoJS (Default)

GeoJS is a free service with no API key required.

Geoip::driver('geojs')->get('8.8.8.8');

IPData

IPData requires an API key. Register for an account and add your key to .env:

IPDATA_API_KEY=your_api_key

Then use the driver:

Geoip::driver('ip-data')->get('8.8.8.8');

Note: IPData offers 1,500 free requests per day. Higher usage requires a paid plan.

Testing

vendor/bin/phpunit

Credits

License

The MIT License (MIT). Please see License File for more information.

About

A Laravel package to get IP addresses geographical location.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages