我正在使用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供应商中提到的代码部分:
/**
* 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];
}我的代码是:
$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';
}我完全是菜鸟,但我知道我需要在某个地方实现这个TRY-CATCH:https://www.w3schools.com/php/php_exception.asp
有没有人能告诉我是怎么做的,是哪一部分?我想在最后一段代码中。但我不确定这些供应商部件到底是如何工作的,因为已经有一些例外。
非常感谢。
发布于 2021-09-30 21:24:40
像这样的东西
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';
}https://stackoverflow.com/questions/69398633
复制相似问题