项目作者: gupalo

项目描述 :
GeoIp client
高级语言: PHP
项目地址: git://github.com/gupalo/geoip.git
创建时间: 2020-01-02T17:48:50Z
项目社区:https://github.com/gupalo/geoip

开源协议:MIT License

下载


GeoIP Client

Adapter for MaxMind and Sypex GeoIP database clients.

Usage

  1. 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:

  1. export MAX_MIND_LICENSE_KEY="XXXXXXXXXXXXXXXX" # you need to register to get the key
  2. export TARGET_DIR="/opt/geoip" # or any other folder where you keep geoip files
  3. curl "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=${MAX_MIND_LICENSE_KEY}" | tar zxf - -C ${TARGET_DIR}/ --strip-components=1 --no-anchored --wildcards *.mmdb
  4. curl "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz&license_key=${MAX_MIND_LICENSE_KEY}" | tar zxf - -C ${TARGET_DIR}/ --strip-components=1 --no-anchored --wildcards *.mmdb

Usage:

  1. $parser = new GeoIpParser($dir);
  2. $geoIp = $parser->parse('1.1.1.1');
  3. $ip = $geoip->getIp();
  4. $isValidIp = $geoip->isValidIp();
  5. $city = $geoip->getCity();
  6. $countryCode = $geoip->getCountryCode();
  7. $country = $geoip->getCountry();
  8. $continentCode = $geoip->getContinentCode();
  9. $postalCode = $geoip->getPostalCode();
  10. $domain = $geoip->getDomain();
  11. $asnNumber = $geoip->getAsnNumber();
  12. $asnOrganization = $geoip->getAsnOrganization();
  13. $isp = $geoip->getIsp();
  14. $organization = $geoip->getOrganization();
  15. $latitude = $geoip->getLatitude();
  16. $longitude = $geoip->getLongitude();
  17. $region = $geoip->getRegion();
  18. $timezone = $geoip->getTimezone();
  19. $currencyCode = $geoip->getCurrencyCode();
  20. $cityPopulation = $geoip->getCityPopulation();
  21. $cityTel = $geoip->getCityTel();
  22. $regionAuto = $geoip->getRegionAuto();
  23. $countryArea = $geoip->getCountryArea();
  24. $countryPopulation = $geoip->getCountryPopulation();
  25. $countryCapital = $geoip->getCountryCapital();
  26. $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:

  1. $parser = new GeoIpParser($dir);
  2. $geoIp = $parser->parseAdvanced('1.1.1.1');
  3. $latitudes = [
  4. $geoIp->getSypexCityLatitude(),
  5. $geoIp->getSypexRegionLatitude(),
  6. $geoIp->getSypexCountryLatitude(),
  7. $geoIp->getMaxMindCityLatitude(),
  8. ];

Also you can get raw data from:

  1. $parser = new GeoIpParser($dir);
  2. $geoIp = $parser->parseAdvanced('1.1.1.1');
  3. $raw = [
  4. $geoIp->getSypexCityRaw(),
  5. $geoIp->getMaxMindCountryRaw(),
  6. $geoIp->getMaxMindCityRaw(),
  7. $geoIp->getMaxMindDomainRaw(),
  8. $geoIp->getMaxMindIspRaw(),
  9. ];

Symfony

Add to config/services.yaml

  1. parameters:
  2. ...
  3. env(GEOIP_DIR): '%kernel.project_dir%/data/geoip'
  4. services:
  5. ...
  6. Gupalo\GeoIp\GeoIpParser:
  7. arguments: ['%env(resolve:GEOIP_DIR)%']
  8. Gupalo\GeoIp\Twig\GeoIpExtension:
  9. tags: ['twig.extension']

Use with autowire

  1. /**
  2. * @Route("/test", name="test")
  3. */
  4. public function test(GeoIpParser $geoIpParser): Response
  5. {
  6. dd($geoIpParser->parse('1.1.1.1'));
  7. }

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

  • domain_ip: convert domain name or IP to IP - 'google.com'|domain_ip, '1.1.1.1'|domain_ip
  • ip_country_code: convert IP to country code - '1.1.1.1'|ip_country_code - US, AU, …
  • ip_country: convert IP to country name - '1.1.1.1'|ip_country - Australia, Russia, …
  • ip_flag: convert IP to HTML with flag - '1.1.1.1'|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:

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