2017 © Pedro Peláez
 

library location

Retrieve a users location by their IP Address using Laravel 5

image

stevebauman/location

Retrieve a users location by their IP Address using Laravel 5

  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 46 Forks
  • 4 Open issues
  • 31 Versions
  • 13 % Grown

The README.md

Location

Retrieve a visitor's location from their IP address using various services (online & local). , (*1)

, (*2)

Requirements

  • PHP >= 8.1
  • Laravel >= 8.0

Installation

Install location using composer require:, (*3)

composer require stevebauman/location

Publish the configuration file (this will create a location.php file inside the config directory):, (*4)

php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"

Usage

Retrieve a client's location

use Stevebauman\Location\Facades\Location;

if ($position = Location::get()) {
    // Successfully retrieved position.
    echo $position->countryName;
} else {
    // Failed retrieving position.
}

Important: - This method retrieves the user's IP address via request()->ip(). - In the default configuration, testing.enabled is active, the returned IP address is in the USA. Disable it to get the client's real IP address., (*5)

Retrieve the location of a specific IP address

$position = Location::get('192.168.1.1');

Testing

You may call Location::fake with an array of IP address patterns and their expected positions to fake the location of an IP address:, (*6)

use Stevebauman\Location\Position;
use Stevebauman\Location\Facades\Location;

Location::fake([
    '127.0.0.1' => Position::make([
        'countryName' => 'United States',
        'countryCode' => 'US',
        // ...
    ])
]);

// Somewhere in your application...

$position = Location::get('127.0.0.1'); // Position

If you prefer, you may use an asterisk to return the same position for any IP address that is given:, (*7)

Location::fake([
    '*' => Position::make([
        'countryName' => 'United States',
        'countryCode' => 'US',
        // ...
    ])
]);

$position = Location::get($anyIpAddress); // Position

If no expectations are given, or an expectation is not matched, Location::get will return false:, (*8)

Location::fake();

Location::get($anyIpAddress); // false

If your application attempts to retrieve the location's of multiple IP addresses, you may provide multiple IP address expectation patterns:, (*9)

Location::fake([
    '127.0.0.1' => Position::make([
        'countryName' => 'United States',
        'countryCode' => 'US',
        // ...
    ]),
    '192.168.1.1' => Position::make([
        'countryName' => 'Canada',
        'countryCode' => 'CA',
        // ...
    ]),
]);

Drivers

Available Drivers

Available drivers:, (*10)

Setting up MaxMind with a self-hosted database (optional)

It is encouraged to set up MaxMind as a fallback driver using a local database so that some location information is returned in the event of hitting a rate limit from the external web services., (*11)

To set up MaxMind to retrieve the user's location from your own server, you must:, (*12)

  1. Create a maxmind account and sign in
  2. Click "Manage License Keys" from the profile menu dropdown
  3. Generate a new license key
  4. Copy the license key and save it into your .env file using the MAXMIND_LICENSE_KEY key
  5. Run php artisan location:update to download the latest .mmdb file into your database/maxmind directory
  6. That's it, you're all set!

Note: Keep in mind, you'll need to update this file by running location:update on a regular basis to retrieve the most current information from your visitors., (*13)

Fallback Drivers

In the config file, you can specify as many fallback drivers as you wish. It is recommended to configure the MaxMind driver with the local database .mmdb file (mentioned above), so you are alwaysretrieving some generic location information from the visitor., (*14)

If an exception occurs trying to grab a driver (such as a 400/500 error if the providers API changes), it will automatically use the next driver in line., (*15)

Creating your own drivers

To create your own driver, simply create a class in your application, and extend the abstract Driver:, (*16)

namespace App\Location\Drivers;

use Illuminate\Support\Fluent;
use Illuminate\Support\Facades\Http;
use Stevebauman\Location\Position;
use Stevebauman\Location\Request;
use Stevebauman\Location\Drivers\Driver;

class MyDriver extends Driver
{    
    protected function process(Request $request): Fluent
    {
         $response = Http::get("https://driver-url.com", ['ip' => $request->getIp()]);

         return new Fluent($response->json());
    }

    protected function hydrate(Position $position, Fluent $location): Position
    {
        $position->countryCode = $location->country_code;

        return $position;
    }
}

Then, insert your driver class name into the configuration file:, (*17)

// config/location.php

'driver' => App\Location\Drivers\MyDriver::class,

Upgrading from v6

In version 7, the codebase has been updated with strict PHP types, updated PHP and Laravel version requirements, an updated Driver structure, as well as a small configuration addition., (*18)

Configuration

In version 7, location drivers can now implement an Updatable interface that allows them to be updated using the location:update command. Currently, only the MaxMind driver supports this., (*19)

To update your configuration file to be able to download the latest MaxMind database file automatically, insert the below url configuration option in your config/location.php file:, (*20)

// config/location.php

return [
    'maxmind' => [
        // ...

        'local' => [
            // ...

+            'url' => sprintf('https://download.maxmind.com/app/geoip_download_by_token?edition_id=GeoLite2-City&license_key=%s&suffix=tar.gz', env('MAXMIND_LICENSE_KEY')),
        ],
    ],
];

Once done, you may execute the below artisan command to download the latest .mmdb file:, (*21)

php artisan location:update

Drivers

In version 7, the codebase has been updated with strict PHP types, updated PHP and Laravel version requirements, and an updated Driver structure., (*22)

If you have created your own custom driver implementation, you must update it to use the base Driver or HttpDriver class., (*23)

If you're fetching a location using an HTTP service, it may be useful to extend the HttpDriver to reduce the code you need to write:, (*24)

namespace App\Location\Drivers;

use Illuminate\Support\Fluent;
use Stevebauman\Location\Position;
- use Stevebauman\Location\Drivers\Driver;
+ use Stevebauman\Location\Drivers\HttpDriver;

- class MyDriver extends Driver
+ class MyDriver extends HttpDriver
{
-    public function url($ip)
+    public function url(string $ip): string;
    {
        return "http://driver-url.com?ip=$ip";
    }

-    protected function process($ip)
-    {
-        return rescue(function () use ($ip) {
-            $response = json_decode(file_get_contents($this->url($ip)), true);
-            
-            return new Fluent($response);
-        }, $rescue = false);
-    }

-    protected function hydrate(Position $position, Fluent $location)
+    protected function hydrate(Position $position, Fluent $location): Position;
    {
        $position->countryCode = $location->country_code;

        return $position;
    }
}

Versioning

Location is versioned under the Semantic Versioning guidelines as much as possible., (*25)

Releases will be numbered with the following format:, (*26)

<major>.<minor>.<patch>

And constructed with the following guidelines:, (*27)

  • Breaking backward compatibility bumps the major and resets the minor and patch.
  • New additions without breaking backward compatibility bumps the minor and resets the patch.
  • Bug fixes and misc changes bumps the patch.

Minor versions are not maintained individually, and you're encouraged to upgrade through to the next minor version., (*28)

Major versions are maintained individually through separate branches., (*29)

The Versions

17/04 2018

dev-master

9999999-dev

Retrieve a users location by their IP Address using Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

17/04 2018

v3.0.1

3.0.1.0

Retrieve a users location by their IP Address using Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

02/03 2018

v3.0.0

3.0.0.0

Retrieve a users location by their IP Address using Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

26/01 2017

v2.0.1

2.0.1.0

Retrieve a users location by their IP Address using Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

20/09 2016

v2.0.0

2.0.0.0

Retrieve a users location by their IP Address using Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

01/12 2015

1.1.x-dev

1.1.9999999.9999999-dev

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

01/12 2015

v1.1.10

1.1.10.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

01/12 2015

v1.3.1

1.3.1.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

16/11 2015

v1.3.0

1.3.0.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

12/10 2015

v1.2.2

1.2.2.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

06/10 2015

v1.2.1

1.2.1.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

14/08 2015

v1.2.0

1.2.0.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

30/05 2015

v1.1.9

1.1.9.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

08/05 2015

v1.1.8

1.1.8.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

06/05 2015

v1.1.7

1.1.7.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

30/04 2015

v1.1.6

1.1.6.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

30/04 2015

v1.1.5

1.1.5.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

30/04 2015

v1.1.4

1.1.4.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

24/04 2015

v1.1.3

1.1.3.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

21/03 2015

v1.1.2

1.1.2.0

Retrieve a users location by their IP Address using Laravel 4 / 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steve Bauman

laravel geoip php geo ip location geo-location

15/03 2015

v1.1.1

1.1.1.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

13/02 2015

v1.1.0

1.1.0.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

09/02 2015

v1.0.7

1.0.7.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

05/01 2015

v1.0.6

1.0.6.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

05/01 2015

v1.0.5

1.0.5.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

29/12 2014

v1.0.4

1.0.4.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

28/12 2014

v1.0.3

1.0.3.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

22/12 2014

v1.0.2

1.0.2.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

08/12 2014

v1.0.1

1.0.1.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

08/12 2014

v1.0

1.0.0.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location geo-location

06/10 2014

0.5

0.5.0.0

Detect user Location by their IP Address

  Sources   Download

MIT

The Requires

 

by Steve Bauman

laravel geoip php ip location