maxmind / GeoIP2-php

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

mostSpecificSubdivision->isoCode is empty #177

Closed mattmary closed 2 years ago

mattmary commented 2 years ago

Hi support,

I try to get the iso code for the region of an IP. mostSpecificSubdivision->name returns the good value, but when I try to get the isoCode, the value is always empty. Is it a bug? What am I doing wrong? ` $geoip = Geoip2::getInstance(); $geoip->getRegionCode('222.213.14.33'); class Geoip2 {

private static $instance = null;

private $geoip;

protected static $records = array();

private function __construct() {
    $this->geoip = new Reader('/usr/local/src/DBIP-City.mmdb');
}

/**
 * @return Geoip2
 */
public static function getInstance() {
    if (is_null(self::$instance)) {
        self::$instance = new self();
    }
    return self::$instance;
}

/**
 * @param $ip
 *
 * @return \GeoIp2\Model\City|mixed
 * @throws \GeoIp2\Exception\AddressNotFoundException
 * @throws \MaxMind\Db\Reader\InvalidDatabaseException
 */
private function getRecord($ip) {
    if (!array_key_exists($ip, self::$records)) {
        self::$records[$ip] =  $this->geoip->city($ip);
    }
    return self::$records[$ip];
}

public function getCountryCode($ip) {
    try {
        $record = $this->getRecord($ip);
        return strtolower($record->country->isoCode);
    } catch (\Exception $e) {
        return 'us';
    }
}

public function getRegionCode($ip) {
    try {
        $record = $this->getRecord($ip);
        return $record->mostSpecificSubdivision->isoCode.'|'.$this->getCountryCode($ip);
    } catch (\Exception $e) {
        return '|us';
    }
}

} `

oschwald commented 2 years ago

It appears the DBIP database you are using does not have an ISO code for that subdivision. You will need to follow up with DBIP to see if that is expected.