server/app/Utils/SystemHelper.php

64 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Utils;
class SystemHelper
{
/**
* 获取服务器信息
*
* @return array
*/
public static function getServerInfo(): array
{
return [
'php_version' => PHP_VERSION,
'server_name' => self::getServerName(),
'server_ip' => self::getServerIp(),
'os' => self::getOs(),
'architecture'=> self::getArchitecture(),
];
}
/**
* 获取服务器主机名
*/
public static function getServerName(): string
{
return gethostname() ?: php_uname('n');
}
/**
* 获取服务器 IP
*/
public static function getServerIp(): string
{
// 尝试解析本机 IP排除 127.0.0.1
$hostname = gethostname();
$ip = gethostbyname($hostname);
if ($ip && $ip !== $hostname && $ip !== '127.0.0.1') {
return $ip;
}
// 如果在容器环境,可能要用命令获取真实 IP
$ip = shell_exec("hostname -I 2>/dev/null | awk '{print $1}'");
return trim($ip) ?: '127.0.0.1';
}
/**
* 获取操作系统信息
*/
public static function getOs(): string
{
return php_uname('s') . ' ' . php_uname('r') . ' (' . php_uname('v') . ')';
}
/**
* 获取系统架构
*/
public static function getArchitecture(): string
{
return php_uname('m'); // 常见值: x86_64, aarch64
}
}