Maxmind GeoIP2教程(操作方法)?

T.T*_*dua 2 maxmind geoip2

我使用的是GeoIp,带有纯PHP代码。.但是GeoIp2变成了命名空间,依此类推,此刻,我无法找到如何使用它的方法。.我已经下载了GeoLite2-Country.mmdb,现在如何获取IP的国家名称,即123.123.123.123

ps我没有GIT / COMPOSER等。

T.T*_*dua 5

我的操作方式:假设创建一个名为“ My_Folder ” 的文件夹,并在其中:

1)创建文件夹GeoIp2,并将此“ SRC”文件夹的内容放入其中(下载)。
2)放置MaxMind文件夹(从“ SRC”文件夹下载)。
3)放置即GeoLite2-Country.mmdb下载)。

然后,在My_Folder中 创建一个example.php文件,并放置以下代码:

$user_ip='123.123.123.123';

spl_autoload_register('func888'); function func888($class){ include_once(str_replace(array('/','\\'), DIRECTORY_SEPARATOR, dirname(__file__)."/$class.php")) ;}
use GeoIp2\Database\Reader; 
//you can do it for "city" too.. just everywhere change phrase "country" with "city".
try{
    $reader = new Reader(dirname(__file__)."/GeoLite2-Country.mmdb");
    $record = $reader->country($user_ip);
    $reader->close();
    $country_name =  $record->raw['country']['names']['en'];
} catch ( GeoIp2\Exception\AddressNotFoundException $e ){    $country_name = 'not_found';  }

echo $country_name;
// RESULTS -------------- > China
Run Code Online (Sandbox Code Playgroud)

ps其他示例位于:https : //github.com/maxmind/GeoIP2-php