survos / ip2location-bundle
Symfony wrapper for ip2location.ip
Package info
github.com/survos/Ip2LocationBundle
Type:symfony-bundle
pkg:composer/survos/ip2location-bundle
Fund package maintenance!
2.0.83
2025-10-27 12:45 UTC
Requires
- php: ^8.4
- ip2location/ip2location-io-php: ^1.0
- symfony/config: ^7.3||^8.0
- symfony/dependency-injection: ^7.3||^8.0
- symfony/http-kernel: ^7.3||^8.0
- twig/twig: ^3.4
Requires (Dev)
- phpstan/phpstan: ^2.0
- survos/step-bundle: dev-main
- symfony/browser-kit: ^7.3||^8.0
- symfony/cache-contracts: ^3.4
- symfony/framework-bundle: ^7.3||^8.0
- symfony/phpunit-bridge: ^7.3||^8.0
- symfony/twig-bundle: ^7.3||^8.0
- symfony/var-dumper: ^7.3||^8.0
Suggests
- symfony/cache-contracts: Cache the lookups
This package is auto-updated.
Last update: 2026-06-09 12:43:51 UTC
README
Symfony Bundle for the ip2location/ip2location-io-php library, to get data from an IP address.
First, get an API key at https://www.ip2location.io/pricing and add it to .env.local
echo "IP2LOCATION_API_KEY=my-api-key" >> .env.local composer req survos/ip2location-bundle bin/console debug:config survos_ip2_location --format=yaml > config/packages/survos_ip2_location.yaml
{{ ipGeolocation(app.request.clientIp).country_code}}
By default, the bundle gets the API key from the environment. Since localhost doesn't have geolocation data, you can set a default. If you're running locally, the remote address is localhost. Get your real IP address at whatismyip.com or https://api.ipify.org?format=json
# config/packages/survos_ip2location.yaml survos_ip2_location: api_key: '%env(IP2LOCATION_API_KEY)%' localhost_ip: 8.8.8.8
Trivial but functional application
Requirements:
- Locally installed PHP 8
- Symfony CLI
- sed (to change /app to / without opening an editor)
- API Key
symfony new Ip2locationDemo --webapp && cd Ip2locationDemo echo "IP2LOCATION_API_KEY=my-api-key" >> .env.local symfony composer req survos/ip2location-bundle symfony console make:controller AppController sed -i "s|/app|/|" src/Controller/AppController.php cat <<'EOF' > templates/app/index.html.twig {% extends 'base.html.twig' %} {% block body %} {% set ip = app.request.clientIp %} {{ isLocalhost(ip) ? "<div>Localhost has no geolocation, using value from config</div>" }} Hello, visitor from {{ ipGeolocation(ip).country_name}} ) <pre>{{ ipGeolocation(ip)|json_encode(constant('JSON_PRETTY_PRINT')) }}</pre> Powered by IP2Location.io <a href="https://www.ip2location.io">IP geolocation</a> web service. {% endblock %} EOF symfony server:start -d symfony open:local