{"id":13712073,"url":"https://github.com/middlewares/geolocation","last_synced_at":"2026-01-07T23:55:01.473Z","repository":{"id":62528423,"uuid":"70470976","full_name":"middlewares/geolocation","owner":"middlewares","description":"PSR-15 middleware to geolocate the client using the ip address","archived":false,"fork":false,"pushed_at":"2021-10-29T16:12:59.000Z","size":29,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-12-19T17:01:53.090Z","etag":null,"topics":["geolocation","http","middleware","psr-15"],"latest_commit_sha":null,"homepage":null,"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/middlewares.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-10T09:06:06.000Z","updated_at":"2023-05-22T12:07:46.000Z","dependencies_parsed_at":"2022-11-02T14:16:58.267Z","dependency_job_id":null,"html_url":"https://github.com/middlewares/geolocation","commit_stats":null,"previous_names":[],"tags_count":8,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fgeolocation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fgeolocation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fgeolocation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fgeolocation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/middlewares","download_url":"https://codeload.github.com/middlewares/geolocation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224535746,"owners_count":17327587,"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":["geolocation","http","middleware","psr-15"],"created_at":"2024-08-02T23:01:14.581Z","updated_at":"2026-01-07T23:55:01.467Z","avatar_url":"https://github.com/middlewares.png","language":"PHP","readme":"# middlewares/geolocation\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE)\n![Testing][ico-ga]\n[![Total Downloads][ico-downloads]][link-downloads]\n[![SensioLabs Insight][ico-sensiolabs]][link-sensiolabs]\n\nMiddleware to geolocate the client using the ip address and [Geocoder](https://github.com/geocoder-php/Geocoder) and save the result as a request attribute.\n\n## Requirements\n\n* PHP \u003e= 7.0\n* A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)\n* A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)\n* A [PHP-HTTP adapter](http://docs.php-http.org/en/latest/clients.html), for example [Guzzle6](https://github.com/php-http/guzzle6-adapter)\n* A [Geocoder ip-based provider](https://github.com/geocoder-php/Geocoder#ip)\n\n## Installation\n\nThis package is installable and autoloadable via Composer as [middlewares/geolocation](https://packagist.org/packages/middlewares/geolocation).\n\n```sh\ncomposer require middlewares/geolocation\n```\n\n## Example\n\n```php\n$freeGeoIpProvider = new Geocoder\\Provider\\FreeGeoIp($adapter);\n\n$dispatcher = new Dispatcher([\n    new Middlewares\\Geolocation($freeGeoIpProvider),\n\n    function ($request) {\n        //Get the client location\n        $location = $request-\u003egetAttribute('client-location');\n\n        $country = $location-\u003efirst()-\u003egetCountry();\n    }\n]);\n\n$response = $dispatcher-\u003edispatch(new ServerRequest());\n```\n\n## Options\n\n#### `__construct(Geocoder\\Provider\\Provider $provider)`\n\nThe geocoder provider used to geolocate the client.\n\nIt's also recommended to configure it to [caching responses.](https://github.com/geocoder-php/Geocoder/blob/master/docs/cookbook/cache.md)\n\n#### `ipAttribute(string $ipAttribute)`\n\nBy default uses the `REMOTE_ADDR` server parameter to get the client ip. This option allows to use a request attribute. Useful to combine with a ip detection middleware, for example [client-ip](https://github.com/middlewares/client-ip).\n\n#### `attribute(string $attribute)`\n\nThe attribute name used to store the client addresses in the server request. By default is `client-location`.\n\n---\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.\n\nThe MIT License (MIT). Please see [LICENSE](LICENSE) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/middlewares/geolocation.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-ga]: https://github.com/middlewares/geolocation/workflows/testing/badge.svg\n[ico-downloads]: https://img.shields.io/packagist/dt/middlewares/geolocation.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/middlewares/geolocation\n[link-downloads]: https://packagist.org/packages/middlewares/geolocation\n[link-sensiolabs]: https://insight.sensiolabs.com/projects/b6c8bd3a-b3da-45ec-b2ac-9d27ae390b1b\n","funding_links":[],"categories":["Packages"],"sub_categories":["Miscellaneous"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Fgeolocation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiddlewares%2Fgeolocation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Fgeolocation/lists"}