Get a localized name of the users city via Maxmind

2019-06-08 09:02发布

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?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-08 09:31

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

Geolocate IP Address

查看更多
闹够了就滚
3楼-- · 2019-06-08 09:32

Here's how to do it without Composer.

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
查看更多
男人必须洒脱
4楼-- · 2019-06-08 09:37

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");
查看更多
登录 后发表回答