maxmind / GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
https://maxmind.github.io/GeoIP2-php/
Apache License 2.0
2.33k stars 276 forks source link

I need city #49

Closed shubaivan closed 9 years ago

shubaivan commented 9 years ago

HelloI need find city for ip, I install maxmind/GeoIP2-php and download file GeoLite2-City.mmdb 31mb GeoLite2-City.mmdb 2,2mb and create service AdditionalFunction and function getInfoIpCity, getInfoIpCountry and when developer registration I use this function like this:

 $ip = $request->getClientIp();
 $record = $hAid->getInfoIpCountry($ip);
 $get_record = $hAid->getInfoIpCity($ip);
 $record_coutry = $record->country->name;

and service:

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;
 }
 }

coutry find right but in dump I have $c = null, $b = null why???? I need city

oschwald commented 9 years ago

What IP are you looking up? We do not have city (or any other particular field) for every IP address.

shubaivan commented 9 years ago

176.241.128.140 there ip with whom exactly will it work? I download GeoLite2-City-CSV_20150707/ and this file GeoLite2-City-Blocks-IPv4.csv 135.3 mb and in this file I find 176.241.128.0/20,690791,690791,,0,0,,50.4500,30.5233 If I have this file how I can find name city ?

oschwald commented 9 years ago

You are correct, GeoLite2 does not appear to have city information for that IP. If you are looking for an IP with city information, you could test 128.101.101.101, for instance.

Please see our dev site for documentation on the CSV format.