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 IP addresses geographical location.

Installation

This package can be installed through Composer.

composer require reefki/laravel-geoip

Optionally, you can publish the config file of this package with this command:

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

Basic usage

Using facade to get information of an IP address:

use Reefki\Geoip\Geoip;

$geoip = Geoip::get('8.8.8.8');
// Or
$geoip = Geoip::get('2001:4860:4860:0:0:0:0:8888');

$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/Chicago
$geoip->latitude; // 37.751
$geoip->longitude; // -97.822
$geoip->cached; // true or false

By default the result will be cached, to get realtime data you can pass a boolean value as the second parameter:

use Reefki\Geoip\Geoip;

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

Get visitor's IP address via request:

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

Get visitor's anonymized IP address:

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

Drivers

Laravel GeoIP support multiple drivers. By default, geojs driver will be used if you don't specify the driver when using the facade or when calling geoip() method via Laravel request instance.

You can change the default driver in your .env file as:

GEOIP_DEFAULT_DRIVER=geojs

Here are the drivers currently available for use.

This is a completly free service, you don't have to do anything to get it to work. Simply pass geojs to the driver paremeter.

use Reefki\Geoip\Geoip;

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

To use this driver, register an account and put the API key in your .env file as:

IPDATA_API_KEY=YOUR_API_KEY

To get the IP information using this driver you can pass ip-data to the driver parameter:

use Reefki\Geoip\Geoip;

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

Please note: IPData offers 1500 daily free requests. If your daily usage is more than 1500 you need to upgrade to one of their paid plan.

Testing

Run the tests with:

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