Get a localized name of the users city via Maxmind GeoLite2 Free

怎甘沉沦 提交于 2019-12-08 05:29:57

问题


i want to show the german name of the users city. Is it possible with the free Version of Maxmind Geoip? I did not find a way to open the GeoLite2-City.mmdb or GeoLiteCity.dat, to see which cities are listed, for building my own translation service. How can i open them?


回答1:


The GeoIP Legacy database does not include localized names, but the GeoIP2 (or GeoLite2) database does. You may access the localized name as follows:

<?php
require_once 'vendor/autoload.php';
use GeoIp2\Database\Reader;

$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-City.mmdb');

$record = $reader->city('128.101.101.101');

print($record->country->names['de'] . "\n");

Alternatively, if you would like the reader to default to German and fall back to English when it isn't available, you can set the language in the constructor:

<?php
require_once 'vendor/autoload.php';
use GeoIp2\Database\Reader;

$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-City.mmdb', array('de', 'en'));

$record = $reader->city('128.101.101.101');

print($record->country->name . "\n");



回答2:


Here's how to do it without Composer.

  • Download the zip DB-Reader here: https://github.com/maxmind/MaxMind-DB-Reader-php
  • Download the data file here (city or country or both): http://dev.maxmind.com/geoip/geoip2/geolite2/
  • Uncompress the data files and put them in a new directory, foo.
  • In DB-Reader, copy examples/benchmark.php to foo/benchmark.php.
  • In DB-Reader, copy src/MaxMind/Db to foo/Db.
  • Edit foo/benchmark.php.

Change:

require_once '../vendor/autoload.php';
use MaxMind\Db\Reader;
$reader = new Reader('GeoIP2-City.mmdb');

To:

require_once __DIR__ . '/' . 'Db/Reader.php';
require_once __DIR__ . '/' . 'Db/Reader/Decoder.php';
require_once __DIR__ . '/' . 'Db/Reader/InvalidDatabaseException.php';
require_once __DIR__ . '/' . 'Db/Reader/Metadata.php';
require_once __DIR__ . '/' . 'Db/Reader/Util.php';     // new 2014/09
use MaxMind\Db\Reader;
$mmdb= true ? 'GeoLite2-City.mmdb' : 'GeoLite2-Country.mmdb';
$reader = new Reader( __DIR__  . '/' . $mmdb );

You'll need PHP 5.3+. You even get some savings in code and number of files vs. using Composer. (Some testing code is eliminated, as well as the whole Guzzle structure.) Also, it makes it clearer how namespaces work in PHP as a nice replacement for classes (when classes are used just for name-spacing).

The rest of benchmark.php you can discard and start using $reader->get().

If you do want to benchmark, on most platforms you need to modify the rand() call. Try this:

Change:

$ip = long2ip(rand(0, pow(2, 32) -1));

To:

$n= (float)mt_rand(0, pow(2, 31) - 1);
if (mt_rand(0,1)) $n+= pow(2, 31);
$ip = long2ip($n);

Or just join four mt_rand(0,255)'s with '.', which is probably easier!

........................ Edit 2014/09 ........................

Added 'Db/Reader/Util.php' above.

Version of MaxMind-DB-Reader-php: 1.0.0 (2014-09-22)

Your file structure should look like this:

./benchmark.php
./GeoLite2-City.mmdb
./GeoLite2-Country.mmdb
./Db/Reader.php
./Db/Reader/Decoder.php
./Db/Reader/InvalidDatabaseException.php
./Db/Reader/Metadata.php
./Db/Reader/Util.php



回答3:


I would recommend using the PHP Extension API if you are concerned at all about performance. You can get upwards of 7 million queries per second with the PHP (C API) extension.

I describe how to compile the extension, and how to use the mmdb databases in PHP to get the localized city name here:

Intro to Maxmind GeoLite2 with Kohana PHP



来源:https://stackoverflow.com/questions/21041463/get-a-localized-name-of-the-users-city-via-maxmind-geolite2-free

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!