IP Geolocation Wrapper with Laravel Support
Install via composer
composer require pulkitjalan/ip-geolocation
There is a Laravel service provider and facade available.
Add the following to the providers
array in your config/app.php
PulkitJalan\IPGeolocation\IPGeolocationServiceProvider::class
Next add the following to the aliases
array in your config/app.php
'IPGeolocation' => PulkitJalan\IPGeolocation\Facades\IPGeolocation::class
Next run php artisan vendor:publish --provider="PulkitJalan\IPGeolocation\IPGeolocationServiceProvider" --tag="config"
to publish the config file.
If you are on a PHP version below 8.1 or a Laravel version below 9.0, use an older version of this package.
The ipGeolocation class takes a config array as the first parameter or defaults to using the ip-api
driver.
Example:
<?php
use PulkitJalan\IPGeolocation\IPGeolocation
$ip = new IPGeolocation();
$lat = $ip->getLatitude(); // 51.5141
$lon = $ip->getLongitude(); // -3.1969
To use the ip-api pro service you can set the options in your config.
Pro Example:
$config = [
'driver' => 'ip-api',
'ip-api' => [
'key' => 'YOUR IP-API KEY',
],
];
To use Maxmind database as the driver you can set the options in your config.
Database Example:
$config = [
'driver' => 'maxmind_database',
'maxmind_database' => [
'database' => '/path/to/database.mmdb',
],
];
To use Maxmind api as the driver you can set the options in your config.
Web API Example:
$config = [
'driver' => 'maxmind_api',
'maxmind_api' => [
'user_id' => 'YOUR MAXMIND USER ID',
'license_key' => 'YOUR MAXMIND LICENSE KEY'
],
];
To use the ipstack as the driver set the config.
Example:
$config = [
'driver' => 'ipstack',
'ipstack' => [
'key' => 'YOUR IPSTACK KEY',
'secure' => true, // (optional) use https
],
];
To use this package in Laravel, simply update the config file in config/ip-geolocation.php
to get the same effect. The driver can be set using the IPGEOLOCATION_DRIVER
env.
IPGeolocation will try to determin the ip using the following http headers: HTTP_CLIENT_IP
, HTTP_X_FORWARDED_FOR
, HTTP_X_FORWARDED
, HTTP_FORWARDED_FOR
, HTTP_FORWARDED
, REMOTE_ADDR
in this order. Optionally use the setIp
method to set it.
$ip->setIp('127.0.0.1');
// Laravel
IPGeolocation::setIp('127.0.0.1');
There are a number of available methods to pull out the required information. All methods will return an empty string if data is unavailable.
Get latitude
$ip->getLatitude();
// Laravel
IPGeolocation::getLatitude();
Get longitude
$ip->getLongitude();
// Laravel
IPGeolocation::getLongitude();
Get city
$ip->getCity();
// Laravel
IPGeolocation::getCity();
Get country
$ip->getCountry();
// Laravel
IPGeolocation::getCountry();
Get country code
$ip->getCountryCode();
// Laravel
IPGeolocation::getCountryCode();
Get region
$ip->getRegion();
// Laravel
IPGeolocation::getRegion();
Get region code
$ip->getRegionCode();
// Laravel
IPGeolocation::getRegionCode();
Get postal code
$ip->getPostalCode();
// Laravel
IPGeolocation::getPostalCode();
Get timezone
$ip->getTimezone();
// Laravel
IPGeolocation::getTimezone();
Get isp (not supported on all drivers)
$ip->getIsp();
// Laravel
IPGeolocation::getIsp();
Get all geo information
$ip->get(); // returns array
// Laravel
IPGeolocation::get(); // returns array
Get raw geo information
$ip->getRaw(); // different drivers will return different data types
// Laravel
IPGeolocation::getRaw(); // different drivers will return different data types
There is an update command available to help with updating and installing a local ip geolocation database. The following will download and install/update the database file to /path/to/database.mmdb
. As of 30th December 2019, Maxmind requires users to create an account and use a license key to download the databases.
<?php
use PulkitJalan\IPGeolocation\IPGeolocationUpdater
$config = [
'driver' => 'maxmind_database',
'maxmind_database' => [
'database' => '/path/to/database.mmdb',
'license_key' => 'YOUR MAXMIND LICENSE KEY'
],
];
(new IPGeolocationUpdater($config))->update();
Once you have registered the service provider (supports auto discovery), you can use the command php artisan ip-geolocation:update
IP-API is a free (or paid) service that can also be used instead of the database file or the paid maxmind service. They do have some limitations on the free service so please have a look at the docs first.
You can use the free database from maxmind (license_key required) or their web api service. You can signup and get a free license code here.