本文介绍了Maxmind GeoIP2教程(操作方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是GeoIp,带有纯PHP代码.但是GeoIp2变成了命名空间等等,这时我无法找到使用方法..我已经下载了GeoLite2-Country.mmdb,现在又下载了如何获取IP的国家/地区名称,即123.123.123.123.

I used GeoIp, with pure PHP codes..but GeoIp2 become namespaced and etc, and at this moment i couldnt find out how to use that.. i have downloaded GeoLite2-Country.mmdb, and now how to get the country name for IP, i.e. 123.123.123.123.

p.s.我没有GIT/COMPOSER等.

p.s. I dont have GIT/COMPOSER or etc..

推荐答案

我是如何做到的:假设在其中创建一个名为" My_Folder "的文件夹:

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

How i did it: let's say, create a folder named "My_Folder" and inside it:

1) create folder GeoIp2 and put in it content of this "SRC" folder (download).
2) put MaxMind folder (download, from "SRC" folder).
3) place i.e. GeoLite2-Country.mmdb (download).

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

then, in My_Folder create a example.php file and put this code:

$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

p.s.在以下位置找到了其他示例: https://github.com/maxmind/GeoIP2-php

p.s. other examples found at:https://github.com/maxmind/GeoIP2-php

这篇关于Maxmind GeoIP2教程(操作方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 16:18