Php 带GeoIp2的Symfony
HelloI需要查找ip的城市,我安装maxmind/GeoIP2 php并下载文件GeoLite2-city.mmdb 31mb GeoLite2-city.mmdb 2,2mb并创建服务附加函数和函数getInfoIpCity、getInfoIpCountry,当开发者注册时,我使用以下函数:Php 带GeoIp2的Symfony,php,symfony,service,doctrine-orm,Php,Symfony,Service,Doctrine Orm,HelloI需要查找ip的城市,我安装maxmind/GeoIP2 php并下载文件GeoLite2-city.mmdb 31mb GeoLite2-city.mmdb 2,2mb并创建服务附加函数和函数getInfoIpCity、getInfoIpCountry,当开发者注册时,我使用以下函数: $ip = $request->getClientIp(); $record = $hAid->getInfoIpCountry($ip); $get_record = $hAid->
$ip = $request->getClientIp();
$record = $hAid->getInfoIpCountry($ip);
$get_record = $hAid->getInfoIpCity($ip);
$record_coutry = $record->country->name;
我下载了GeoLite2-City-CSV_20150707/和这个文件GeoLite2-City-Blocks-IPv4.CSV 135.3 mb,在这个文件中我找到了176.241.128.0/206907916907691,0,0,50.4500,30.5233如果我有这个文件,我如何找到城市的名字?如果我在变量50.4500,30.5233和谷歌知道这是基辅,我如何得到城市的名称?
和服务:
class AdditionalFunction
{
private $rootDir;
public function setRootDir($rootDir)
{
$this->rootDir = $rootDir;
}
public function getInfoIpCountry($ip)
{
try {
$reader = new Reader($this->rootDir.'/data/GeoLite2-Country.mmdb');
$data = $reader->country($ip);
} catch (\Exception $e) {
$data = null;
}
return $data;
}
public function getInfoIpCity($ip)
{
try {
$reader = new Reader($this->rootDir.'/data/GeoLite2-City.mmdb');
$data = $reader->city($ip);
$m = $data->country->name;
$t = $data->country->isoCode;
$b = $data->mostSpecificSubdivision->name;
$c = $data->city->name;
} catch (\Exception $e) {
$data = null;
}
return $data;
}
}
找到正确的答案
但在dump中,我有$c=null,$b=null
为什么?我需要城市我解决了我的问题,我为maxmind使用了free base,但如果在该数据库中找不到城市,我将使用googlr aip并为纬度和经度查找城市,代码如下:
public function getInfoIpCity($ip)
{
try {
$reader = new Reader($this->rootDir.'/data/GeoLite2-City.mmdb');
$data_geo = $reader->city($ip);
$name = $data_geo->country->name;
$isoCode = $data_geo->country->isoCode;
$mostSpecificSubdivision = $data_geo->mostSpecificSubdivision->name;
$data = $city = $data_geo->city->name;
$postal = $data_geo->postal->code; // '55455'
$latitude = $data_geo->location->latitude; // 50.45
$longitude = $data_geo->location->longitude;// 30.5233
if (is_null($data)){
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$latitude.','.$longitude.'&sensor=false';
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata )&& $jsondata ['status'] == "OK")
{
$addr = $jsondata ['results'][0]['address_components'][4]['long_name'];
$addr2 = $jsondata ['results'][0]['address_components'][4]['short_name'];
$addr3 = $jsondata ['results'][0]['address_components'][3]['long_name'];
}
$info = "Country: " . $addr . " | Region: " . $addr2 . " | City: " . $addr3;
$data = $addr3;
}
} catch (\Exception $e) {
$data = null;
}
return $data;
}