我下载并提取了 GeoLite2-City.mmdb、GeoLite2-Country.mmdb。两者都在 htdocs\geoip\
然后,我运行了这个脚本。问题是,这个东西是如何工作的? require_once 'vendor/autoload.php';
应该包含什么?我在这里错过了什么吗?我曾经使用这些作为 .dat 文件的旧版本,并且没有问题。这些 .mmdb 对我来说有点难破解。当用户在页面上使用搜索工具时,我要做的就是获取国家代码、国家名称和其他数据以将其存储在数据库中。我该如何进行?
我的测试页取自网站
<?php
require_once 'vendor/autoload.php'; //What is this supposed to contain?
use GeoIp2\Database\Reader; // What is this too?
// This creates the Reader object, which should be reused across
// lookups.
$reader = new Reader($_SERVER['DOCUMENT_ROOT'].'\geoip\GeoIP2-City.mmdb'); // Where my DB resides
// Replace "city" with the appropriate method for your database, e.g.,
// "country".
$record = $reader->city('128.101.101.101');
print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
print($record->country->names['zh-CN'] . "\n"); // '??'
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
print($record->city->name . "\n"); // 'Minneapolis'
print($record->postal->code . "\n"); // '55455'
print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
?>
最佳答案
如果您对 performance 很担心,我建议您使用 PHP Extension API .使用 PHP (C API) 扩展,您每秒可以获得超过 700 万次查询,而使用纯 PHP API 则为 9,000 qps。
我在这里描述了如何编译扩展,以及如何在 PHP 中使用 mmdb 数据库:
Intro to Maxmind GeoLite2 with Kohana PHP
关于php - 我如何在 php 中使用 MaxMind 的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20652745/