Files
ipcheck/app/controller/Index.php
T
2022-08-23 13:51:25 +08:00

64 lines
1.3 KiB
PHP

<?php
namespace app\controller;
use App\model\Ip2locationDb11Model;
use itbdw\Ip\IpLocation;
use support\Db;
use support\Request;
class Index
{
public function test()
{
echo 'test';
}
public function index(Request $request, $checkIp = null)
{
$ip = $request->getRealIp();
if ($checkIp) {
$ip = gethostbyname($checkIp);
}
$filepath = '/usr/src/myapp/lib/qqwry.dat';
$location = IpLocation::getLocation($ip, $filepath);
return json(['ip' => $ip, 'location' => $location]);
}
public function location(Request $request, $checkIp = null)
{
$ip = $request->getRealIp();
if ($checkIp) {
$ip = gethostbyname($checkIp);
}
$ipNum = ip2long($ip);
$record = Ip2locationDb11Model::query()->where(
'ip_from', '<=', $ipNum
)->where(
'ip_to', '>=', $ipNum
)->first();
if ($record) {
$record = $record->toArray();
$mapUrl = "https://www.google.com/maps/search/{$record['latitude']},{$record['longitude']}";
} else {
$record = [];
$mapUrl = '';
}
return json(['ip' => $ip, 'record' => $record, 'map' => [
'google' => $mapUrl
]], JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
}
}