获取用户ip地址信息

This commit is contained in:
2024-12-04 08:37:28 +08:00
parent b4cec3651b
commit 2363671cd6

View File

@@ -1,4 +1,7 @@
<?php
use function EasyWeChat\Kernel\Support\get_client_ip;
if (!function_exists('ds')) {
function ds($data, $exit = false)
{
@@ -109,6 +112,39 @@ if (!function_exists('get_device_type')) {
$deviceInfo['platform'] = 'Unknown';
}
$deviceInfo['ip'] = cc_get_ip_lookup();
return $deviceInfo;
}
}
/**
* 获取ip所属地
*
* @return true
*/
if ( !function_exists('cc_get_ip_lookup') ) {
function cc_get_ip_lookup($ip = ''): array {
if ( empty($ip) ) {
$ip = get_client_ip();
}
$result = (new GuzzleHttp\Client())->get('https://api.vore.top/api/Weather?ip='. $ip);
$weather = json_decode($result->getBody()->getContents(), true);
if ( empty($weather) ) {
$data = [
'weather' => '未知',
'temperature' => '未知',
'winddirection' => '未知',
'reporttime' => '未知',
'area' => '未知',
'info' => '未知',
];
} else {
$data = [
'area' => $weather['data']['ipdata']['area'],
'info' => $weather['data']['ipdata']['info'],
];
}
return $data;
}
}