You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Malio/app/Utils/GeoIP2.php

38 lines
863 B
PHP

<?php
namespace App\Utils;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
class GeoIP2
{
public function __construct()
{
$this->reader = new Reader(BASE_PATH . '/storage/GeoLite2-City.mmdb');
}
public function getLocation($ip, $lang)
{
try {
$record = $this->reader->city($ip);
} catch (AddressNotFoundException $e) {
if ($lang == 'zh-CN') {
return "未知";
} else {
return "Unknow";
}
}
if ($lang == 'zh-cn') {
$lang = 'zh-CN';
}
if ($lang == 'en') {
return $record->city->name. ', '.$record->country->name;
} else if ($lang == 'zh-CN') {
return $record->country->name[$lang]. ' '.$record->city->name[$lang];
}
}
}