GeoIP client

1.3.2 2022-03-17 10:16 UTC

This package is auto-updated.

Last update: 2022-07-17 11:14:53 UTC


Adapter for MaxMind and Sypex GeoIP database clients.


composer require gupalo/geoip

Put GeoIP databases to some directory. Supported databases:

  • GeoIP2-City.mmdb or GeoLite2-City.mmdb
  • GeoIP2-Country.mmdb or GeoLite2-Country.mmdb
  • GeoIP2-Domain.mmdb
  • GeoIP2-ISP.mmdb
  • SxGeoMax.dat

You can download MaxMind lite databases:

export MAX_MIND_LICENSE_KEY="XXXXXXXXXXXXXXXX" # you need to register to get the key
export TARGET_DIR="/opt/geoip" # or any other folder where you keep geoip files
curl "${MAX_MIND_LICENSE_KEY}" | tar zxf - -C ${TARGET_DIR}/ --strip-components=1 --no-anchored --wildcards *.mmdb
curl "${MAX_MIND_LICENSE_KEY}" | tar zxf - -C ${TARGET_DIR}/ --strip-components=1 --no-anchored --wildcards *.mmdb


$parser = new GeoIpParser($dir);
$geoIp = $parser->parse('');

$ip = $geoip->getIp();
$isValidIp = $geoip->isValidIp(); 
$city = $geoip->getCity(); 
$countryCode = $geoip->getCountryCode(); 
$country = $geoip->getCountry(); 
$continentCode = $geoip->getContinentCode(); 
$postalCode = $geoip->getPostalCode(); 
$domain = $geoip->getDomain(); 
$asnNumber = $geoip->getAsnNumber(); 
$asnOrganization = $geoip->getAsnOrganization(); 
$isp = $geoip->getIsp(); 
$organization = $geoip->getOrganization(); 
$latitude = $geoip->getLatitude(); 
$longitude = $geoip->getLongitude(); 
$region = $geoip->getRegion(); 
$timezone = $geoip->getTimezone(); 
$currencyCode = $geoip->getCurrencyCode(); 
$cityPopulation = $geoip->getCityPopulation(); 
$cityTel = $geoip->getCityTel(); 
$regionAuto = $geoip->getRegionAuto(); 
$countryArea = $geoip->getCountryArea(); 
$countryPopulation = $geoip->getCountryPopulation(); 
$countryCapital = $geoip->getCountryCapital(); 
$countryPhoneCode = $geoip->getCountryPhoneCode();

Advanced Usage

If data may exist in several databases like latitude/longitude then data is taken from the best database:

  • MaxMind City (if not available - MaxMind City Lite)
  • Sypex (if has city data)
  • MaxMind Country (if not available - MaxMind Country Lite)
  • Sypex

You can get data from specific database:

$parser = new GeoIpParser($dir);
$geoIp = $parser->parseAdvanced('');
$latitudes = [

Also you can get raw data from:

$parser = new GeoIpParser($dir);
$geoIp = $parser->parseAdvanced('');
$raw = [


Add to config/services.yaml

    env(GEOIP_DIR): '%kernel.project_dir%/data/geoip'

        arguments: ['%env(resolve:GEOIP_DIR)%']
        tags: ['twig.extension']

Use with autowire

 * @Route("/test", name="test")
public function test(GeoIpParser $geoIpParser): Response

GeoIpExtension is optional to add but if you added it you have Twig filters:

  • domain_ip: convert domain name or IP to IP - ''|domain_ip, ''|domain_ip
  • ip_country_code: convert IP to country code - ''|ip_country_code - US, AU, ...
  • ip_country: convert IP to country name - ''|ip_country - Australia, Russia, ...
  • ip_flag: convert IP to HTML with flag - ''|ip_flag
  • country_code_flag: convert country code to HTML with flag - 'RU'|country_code_flag

To use flags copy public/css/flags.css and public/img/flags.png to your public folder.

Add to base.html.twig or other template:

<link rel="stylesheet" href="{{ asset('css/flags.css') }}">