survos/ip2location-bundle

Symfony wrapper for ip2location.ip

Fund package maintenance!
kbond

Installs: 376

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.5.442 2024-11-24 12:52 UTC

This package is auto-updated.

Last update: 2024-12-15 17:32:19 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