Skip to content

Instantly share code, notes, and snippets.

@tattali
Created January 29, 2023 16:37
Show Gist options
  • Save tattali/a3177b440eb7190401a703c8d91c3f14 to your computer and use it in GitHub Desktop.
Save tattali/a3177b440eb7190401a703c8d91c3f14 to your computer and use it in GitHub Desktop.
Easy install of GeoIP2 with Symfony

GeoIP2 Symfony

Create a free account on https://www.maxmind.com/en/geolite2/signup

And validate it by following the link in your mailbox

Then generate your licence key https://www.maxmind.com/en/accounts/current/license-key

Then put the value in your .env or .env.local

GEOIP2_ACCOUNT_ID=123456
GEOIP2_LICENSE_KEY=xxxxxx

In your config/services.yaml

GeoIp2\WebService\Client:
    arguments:
        $accountId: '%env(int:GEOIP2_ACCOUNT_ID)%'
        $licenseKey: '%env(string:GEOIP2_LICENSE_KEY)%'
        $locales: ['%kernel.default_locale']
        $options:
            host: 'geolite.info'

Then you can access the service as you want, ex :

use GeoIp2\WebService\Client;

public function test(Client $client)
{
    $record = $client->city();

    dump($record->country->isoCode); // 'US'
    dump($record->country->name); // 'United States'
    dump($record->country->names['zh-CN']); // '美国'

    dump($record->mostSpecificSubdivision->name); // 'Minnesota'
    dump($record->mostSpecificSubdivision->isoCode); // 'MN'

    dump($record->city->name); // 'Minneapolis'

    dump($record->postal->code); // '55455'

    dump($record->location->latitude); // 44.9733
    dump($record->location->longitude); // -93.2323

    dump($record->traits->network); // '128.101.101.101/32'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment