1.
http://dev.maxmind.com/geoip/legacy/install/country/
下载ip库,复制到/usr/local/share/GeoIP/
步骤:
wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
mkdir /usr/local/share/GeoIP/
mv GeoIP.dat /usr/local/share/GeoIP/
2.
安装php代码:
http://dev.maxmind.com/geoip/legacy/downloadable/
找到php的guit点击
https://github.com/maxmind/geoip-api-php
点击下载,完成后
执行example的country就可以找到国家了
<?php
include("./geoip_api_php/src/geoip.inc");
class getAllIpInfoJson{
public $_argv = "";
public function __construct(){
$argv = $this->getArgv();
$this->_argv = $argv ;
}
public function run(){
$file_name = $this->_argv[0];
$php_commond = $this->_argv[1];
$ip = $php_commond;
//$ip = "113.89.211.4";
$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat", GEOIP_STANDARD);
if(strstr($ip,":")){
//ipv6
$country_code = geoip_country_code_by_addr_v6($gi,$ip);
$country_name = geoip_country_name_by_addr_v6($gi,$ip);
}else{
$country_code = geoip_country_code_by_addr($gi,$ip);
$country_name = geoip_country_name_by_addr($gi,$ip);
}
echo json_encode(array(
"code"=>$country_code,
"name"=>$country_name,
));
geoip_close($gi);
}
public function getArgv(){
$argv = array();
foreach ($_SERVER['argv'] as $key=>$arg){
$argv[$key] = $arg;
}
return $argv;
}
}
$getAllIpInfoJson = new getAllIpInfoJson();
$getAllIpInfoJson->run();
php getAllIpInfoJson.php 24.24.24.24
结果:
{"code":"US","name":"United States"}
php getAllIpInfoJson.php 24.24.24.24
结果:
{"code":"US","name":"United States"}