简体   繁体   English

例外 - Maxmind geoip2

[英]Exception - Maxmind geoip2

I'm using Maxmind GeoIP2 for monitoring traffic on my websites but my script is stopping on Fatal Error (I changed the IP address on 12.34.56.78):我正在使用 Maxmind GeoIP2 来监控我网站上的流量,但我的脚本因致命错误而停止(我在 12.34.56.78 上更改了 IP 地址):

PHP Notice:  Undefined index: domain in /home/www/parser_only/ins.php on line 15
PHP Notice:  Undefined index: domain in /home/www/parser_only/ins.php on line 15
PHP Fatal error:  Uncaught GeoIp2\Exception\AddressNotFoundException: The address 12.34.56.78 is not in the database. in /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php:248
Stack trace:
#0 /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php(217): GeoIp2\Database\Reader->getRecord('Country', 'Country', '12.34.56.78')
#1 /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php(90): GeoIp2\Database\Reader->modelFor('Country', 'Country', '12.34.56.78')
#2 /home/www/parser_only/ins.php(115): GeoIp2\Database\Reader->country('12.34.56.78')
#3 /home/www/parser_only/ins.php(73): geo('12.34.56.78')
#4 {main}
  thrown in /home/www/parser_only/vendor/geoip2/geoip2/src/Database/Reader.php on line 248
run done

mentioned parts of code in VENDOR: VENDOR中提到的部分代码:

    /**
     * This method returns a GeoIP2 Country model.
     *
     * @param string $ipAddress an IPv4 or IPv6 address as a string
     *
     * @throws \GeoIp2\Exception\AddressNotFoundException  if the address is
     *                                                     not in the database
     * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
     *                                                     is corrupt or invalid
     *
     * @return \GeoIp2\Model\Country
     */
    public function country($ipAddress)
    {
        return $this->modelFor('Country', 'Country', $ipAddress);
    }
    private function modelFor($class, $type, $ipAddress)
    {
        list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);

        $record['traits']['ip_address'] = $ipAddress;
        $record['traits']['prefix_len'] = $prefixLen;

        $class = 'GeoIp2\\Model\\' . $class;

        return new $class($record, $this->locales);
    }
    private function getRecord($class, $type, $ipAddress)
    {
        if (strpos($this->dbType, $type) === false) {
            $method = lcfirst($class);
            throw new \BadMethodCallException(
                "The $method method cannot be used to open a {$this->dbType} database"
            );
        }
        list($record, $prefixLen) = $this->dbReader->getWithPrefixLen($ipAddress);
        if ($record === null) {
            throw new AddressNotFoundException(
                "The address $ipAddress is not in the database."
            );
        }
        if (!\is_array($record)) {
            // This can happen on corrupt databases. Generally,
            // MaxMind\Db\Reader will throw a
            // MaxMind\Db\Reader\InvalidDatabaseException, but occasionally
            // the lookup may result in a record that looks valid but is not
            // an array. This mostly happens when the user is ignoring all
            // exceptions and the more frequent InvalidDatabaseException
            // exceptions go unnoticed.
            throw new InvalidDatabaseException(
                "Expected an array when looking up $ipAddress but received: "
                . \gettype($record)
            );
        }

        return [$record, $prefixLen];
    }

and my code:和我的代码:

$sql .= "('". mysqli_real_escape_string($db, inet_pton($a[2])) ."','". geo($a[2]) ."','". mysqli_real_escape_string($db, trim(substr($a[4], 0, strpos($a[4], 'HTTP')))) ."','". strtotime($a[3]) ."','{$a[6]}','{$a[1]}'),";
function geo($ip){
    $reader = new Reader(__DIR__ . '/GeoLite2-Country.mmdb');

    // Replace "city" with the appropriate method for your database, e.g.,
    // "country".
    $record = $reader->country($ip);

    $geo = $record->country->isoCode . "\n";
    // $geo = geoip_record_by_name($ip);
     if($geo) return $geo;
     else return 'XX';
}

I am total noob, but I understand that I need somewhere implement this TRY-CATCH: https://www.w3schools.com/php/php_exception.asp我完全是菜鸟,但我知道我需要在某个地方实现这个 TRY-CATCH: https://www.w3schools.com/php/php_exception.asp

Can please somebody show me how and in which part?可以请有人告诉我如何以及在哪一部分吗? I guess in the last piece of code.我猜在最后一段代码中。 But I am not sure how exactly these vendor parts work, because there are some exceptions already.但我不确定这些供应商部件究竟是如何工作的,因为已经有一些例外了。

Thanks a lot.多谢。

Something like this像这样的东西

function geo($ip) {
    $reader = new Reader(__DIR__ . '/GeoLite2-Country.mmdb');

    // Replace "city" with the appropriate method for your database, e.g.,
    // "country".
    try {
        $record = $reader->country($ip);
    } catch (\GeoIp2\Exception\AddressNotFoundException) {
        return 'XX';
    }

    $geo = $record->country->isoCode . "\n";
    // $geo = geoip_record_by_name($ip);
     if($geo) return $geo;
     else return 'XX';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM