{"id":23434923,"url":"https://github.com/gpslab/geoip2","last_synced_at":"2025-04-10T01:11:06.300Z","repository":{"id":45256585,"uuid":"79822037","full_name":"gpslab/geoip2","owner":"gpslab","description":"A Symfony Bundle for the Maxmind GeoIP2 API","archived":false,"fork":false,"pushed_at":"2025-03-21T21:14:42.000Z","size":221,"stargazers_count":54,"open_issues_count":8,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-10T01:11:01.697Z","etag":null,"topics":["geoip","maxmind","maxmind-geoip2-api","php","symfony-bundle","symfony-flex"],"latest_commit_sha":null,"homepage":"https://www.maxmind.com/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gpslab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-23T16:20:16.000Z","updated_at":"2025-01-24T21:19:42.000Z","dependencies_parsed_at":"2022-09-12T18:50:43.764Z","dependency_job_id":null,"html_url":"https://github.com/gpslab/geoip2","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpslab%2Fgeoip2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpslab%2Fgeoip2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpslab%2Fgeoip2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpslab%2Fgeoip2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gpslab","download_url":"https://codeload.github.com/gpslab/geoip2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137886,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["geoip","maxmind","maxmind-geoip2-api","php","symfony-bundle","symfony-flex"],"created_at":"2024-12-23T12:34:00.598Z","updated_at":"2025-04-10T01:11:06.276Z","avatar_url":"https://github.com/gpslab.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://img.shields.io/packagist/v/gpslab/geoip2.svg?maxAge=3600\u0026label=stable)](https://packagist.org/packages/gpslab/geoip2)\n[![Test](https://github.com/gpslab/geoip2/actions/workflows/test.yaml/badge.svg)](https://github.com/gpslab/geoip2/actions/workflows/test.yaml)\n[![Coverage Status](https://img.shields.io/coveralls/gpslab/geoip2.svg?maxAge=3600)](https://coveralls.io/github/gpslab/geoip2?branch=master)\n[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/gpslab/geoip2.svg?maxAge=3600)](https://scrutinizer-ci.com/g/gpslab/geoip2/?branch=master)\n[![License](https://img.shields.io/packagist/l/gpslab/geoip2.svg?maxAge=3600)](https://github.com/gpslab/geoip2)\n\nA Symfony Bundle for the Maxmind GeoIP2 API\n===========================================\n\nBundle for use [maxmind/GeoIP2](https://github.com/maxmind/GeoIP2-php) in Symfony.\n\n## Installation\n\nPretty simple with [Composer](http://packagist.org), run:\n\n```sh\ncomposer req gpslab/geoip2\n```\n\n## Configuration\n\nTo configure auto-update the database you need to generate your personal licence key.\n\n#### Steps for generate licence key\n\n1. [Sign up for a MaxMind account](https://www.maxmind.com/en/geolite2/signup) (no purchase required)\n2. Login and generate a [licence key](https://www.maxmind.com/en/accounts/current/license-key)\n3. Save your licence key\n4. Open [download page](https://www.maxmind.com/en/download_files) and find your needed DB edition `ID` and copy value\nfrom first column.\n\nExample configuration:\n\n```yml\ngpslab_geoip:\n    # Your personal licence key\n    license: 'XXXXXXXXXXXXXXXX'\n\n    # One of database edition IDs:\n    #   GeoLite2-ASN\n    #   GeoLite2-City\n    #   GeoLite2-Country\n    #   GeoIP2-City\n    #   GeoIP2-Country\n    #   GeoIP2-Anonymous-IP\n    #   GeoIP2-Domain\n    #   GeoIP2-ISP\n    edition: 'GeoLite2-City'\n```\n\n#### Database source URL\n\nBy default, this URL is used to download a new databases\n`https://download.maxmind.com/app/geoip_download?edition_id={edition_id}\u0026license_key={license_key}\u0026suffix=tar.gz`\n\n* `edition_id` - character ID name from first column on [download page](https://www.maxmind.com/en/download_files);\n* `license_key` - your personal [licence key](https://www.maxmind.com/en/accounts/current/license-key).\n\nYou can change this URL, for example, if you want to use a proxy to download the database. You can customize the source\nURL in the configuration.\n\n```yml\ngpslab_geoip:\n    license: 'XXXXXXXXXXXXXXXX'\n    edition: 'GeoLite2-City'\n    url: 'https://example.com/GeoLite2-City.tar.gz'\n```\n\n### Target download path\n\nBy default, new databases downloaded in `%kernel.cache_dir%/{edition_id}.mmdb`, where `edition_id` is a character ID\nname from first column on [download page](https://www.maxmind.com/en/download_files). That is, by default, the new\ndatabase will be downloaded into folder `var/cache/{env}/`. Keeping the database in the cache folder for each\nenvironment may not be optimal. You can choose a common directory for all environments.\n\n```yml\ngpslab_geoip:\n    license: 'XXXXXXXXXXXXXXXX'\n    edition: 'GeoLite2-City'\n    path: '%kernel.project_dir%/var/GeoLite2-City.mmdb'\n```\n\n#### Localization\n\nBy default, the English locale is used for GeoIP record. You can change the locale for record and declare multiple\nlocales for fallback.\n\n```yml\ngpslab_geoip:\n    license: 'XXXXXXXXXXXXXXXX'\n    edition: 'GeoLite2-City'\n    locales: [ 'ru', 'en' ]\n```\n\n## Usage\n\nYou can get GeoIP2 reader service:\n\n```php\nuse GeoIp2\\Database\\Reader;\n\n// get a GeoIP2 reader\n$reader = $this-\u003eget(Reader::class);\n// or\n//$reader = $this-\u003eget('geoip2.reader');\n\n// get a GeoIP2 City model\n$record = $reader-\u003ecity('128.101.101.101');\n\nprint($record-\u003ecountry-\u003eisoCode . \"\\n\"); // 'US'\nprint($record-\u003ecountry-\u003ename . \"\\n\"); // 'United States'\nprint($record-\u003ecountry-\u003enames['zh-CN'] . \"\\n\"); // '美国'\n\nprint($record-\u003emostSpecificSubdivision-\u003ename . \"\\n\"); // 'Minnesota'\nprint($record-\u003emostSpecificSubdivision-\u003eisoCode . \"\\n\"); // 'MN'\n\nprint($record-\u003ecity-\u003ename . \"\\n\"); // 'Minneapolis'\n\nprint($record-\u003epostal-\u003ecode . \"\\n\"); // '55455'\n\nprint($record-\u003elocation-\u003elatitude . \"\\n\"); // 44.9733\nprint($record-\u003elocation-\u003elongitude . \"\\n\"); // -93.2323\n```\n\nFor more example see the [GeoIP2](https://github.com/maxmind/GeoIP2-php) library.\n\n## Multiple databases\n\nYou can use multiple GeoIP databases in one application. Need update configuration file. \n\n```yml\ngpslab_geoip:\n    databases:\n        default:\n            license: 'XXXXXXXXXXXXXXXX'\n            edition: 'GeoLite2-City'\n        country:\n            license: 'XXXXXXXXXXXXXXXX'\n            edition: 'GeoLite2-Country'\n        asn:\n            license: 'XXXXXXXXXXXXXXXX'\n            edition: 'GeoLite2-ASN'\n```\n\nUsing in application:\n\n```php\n// get a GeoIP2 reader for City database\n$default_reader = $this-\u003eget('geoip2.database.default_reader');\n// or\n//$default_reader = $this-\u003eget(Reader::class);\n// or\n//$default_reader = $this-\u003eget('geoip2.reader');\n\n// get a GeoIP2 reader for Country database\n$country_reader = $this-\u003eget('geoip2.database.country_reader');\n\n// get a GeoIP2 reader for ASN database\n$asn_reader = $this-\u003eget('geoip2.database.asn_reader');\n```\n\nYou can rename the default database.\n\n```yml\ngpslab_geoip:\n    default_database: 'city'\n    databases:\n        asn:\n            license: 'XXXXXXXXXXXXXXXX'\n            edition: 'GeoLite2-ASN'\n        city:\n            license: 'XXXXXXXXXXXXXXXX'\n            edition: 'GeoLite2-City'\n        country:\n            license: 'XXXXXXXXXXXXXXXX'\n            edition: 'GeoLite2-Country'\n```\n\n```php\n// get a GeoIP2 reader for City database\n$default_reader = $this-\u003eget('geoip2.database.city_reader');\n// or\n//$default_reader = $this-\u003eget(Reader::class);\n// or\n//$default_reader = $this-\u003eget('geoip2.reader');\n```\n\nIn order not to repeat the license key and locales for each database, you can specify them once.\n\n```yml\ngpslab_geoip:\n    license: 'XXXXXXXXXXXXXXXX' # global license\n    locales: [ 'ru', 'en' ] # global locales\n    default_database: 'city'\n    databases:\n        asn:\n            edition: 'GeoLite2-ASN'\n            locales: [ 'fr' ] # customize locales\n        city:\n            edition: 'GeoLite2-City'\n            url: 'https://example.com/GeoLite2-City.tar.gz' # customize url\n            path: '%kernel.project_dir%/var/GeoLite2-City.mmdb' # customize path\n        country:\n            edition: 'GeoLite2-Country'\n            license: 'YYYYYYYYYYYYYYYY' # customize license\n```\n\n### GeoIP data in client locale\n\nIf you want to show the GeoIP data to the user and show them in the user locale, then you can use the reader factory.\n\n```php\nuse GpsLab\\Bundle\\GeoIP2Bundle\\Reader\\ReaderFactory;\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\nclass GeoIPController\n{\n    public function index(Request $request, ReaderFactory $factory): Response\n    {\n        $client_locale = $request-\u003egetLocale();\n        $client_ip = $request-\u003egetClientIp();\n        $database_name = 'default';\n        $fallback_locale = 'en';\n    \n        $reader = $factory-\u003ecreate($database_name, [$client_locale, $fallback_locale]);\n        $record = $reader-\u003ecity($client_ip);\n    \n        return new Response(sprintf('You are from %s?', $record-\u003ecountry-\u003ename));\n    }\n}\n```\n\n## Console commands\n\n### Update GeoIP database\n\nExecute console command for update all databases:\n\n```\nphp bin/console geoip2:update\n```\n\nIf you use multiple databases, then for config:\n\n```yml\ngpslab_geoip:\n    # ...\n    databases:\n        asn:\n            # ...\n        city:\n            # ...\n        country:\n            # ...\n```\n\nYou can update several databases:\n\n```\nphp bin/console geoip2:update city country\n```\n\nOptionally installing splitbrain/php-archive uses significantly less memory when updating a database\nand can avoid out of memory errors:\n\n```\ncomposer req splitbrain/php-archive\n```\n\n### Download GeoIP database\n\nYou can download custom database with console command:\n\n```\nphp bin/console geoip2:download https://example.com/GeoLite2-City.tar.gz /path/to/GeoLite2-City.mmdb\n```\n\n## License\n\nThis bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpslab%2Fgeoip2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgpslab%2Fgeoip2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpslab%2Fgeoip2/lists"}