I recently needed a check of to default locales and currencies of . The torann/geoip package by Daniel Stainback fit the bill nicely to provide for 5 applications.

The Laravel GeoIP package supports a few services, including ip-api.com by default, a downloadable Maxmind Database, and the Maxmind API. You can also add a custom service easily through the configuration.

The basic usage of this package is calling the geoip() helper, optionally passing an IP address. The following is an example object that is returned based on IP :

TorannGeoIPLocation {

    #attributes:array [
        'ip'           => '232.223.11.11',
        'iso_code'     => 'US',
        'country'      => 'United States',
        'city'         => 'New Haven',
        'state'        => 'CT',
        'state_name'   => 'Connecticut',
        'postal_code'  => '06510',
        'lat'          => 41.28,
        'lon'          => -72.88,
        'timezone'     => 'America/New_York',
        'continent'    => 'NA',
        'currency'     => 'USD',
        'default'      => false,
    ]
}

As you can see, the Location object has some geo-specific data that is helpful in determining the currency of the user, his or her timezone, and country ISO code.

A configurable fallback location is also available in case the location is not found, and the default parameter will be set to true. You can override the default configuration, which looks like this:

'default_location' => [
    'ip' => '127.0.0.0',
    'iso_code' => 'US',
    'country' => 'United States',
    'city' => 'New Haven',
    'state' => 'CT',
    'state_name' => 'Connecticut',
    'postal_code' => '06510',
    'lat' => 41.31,
    'lon' => -72.92,
    'timezone' => 'America/New_York',
    'continent' => 'NA',
    'default' => true,
    'currency' => 'USD',
],

Check out the package source code on the official GitHub repository, and the documentation to help you get started.



Source link https://laravel-news.com/use-laravel-geoip-to-determine-users-geographical-location

LEAVE A REPLY

Please enter your comment!
Please enter your name here