hyperf框架获取客户端真实ip
use Hyperf\Utils\ApplicationContext;
use Hyperf\HttpServer\Contract\RequestInterface;
function getRealIp(): string
{
$request = ApplicationContext::getContainer()->get(RequestInterface::class);
$headers = $request->getHeaders();
if(isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
return $headers['x-forwarded-for'][0];
} elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
return $headers['x-real-ip'][0];
}
$serverParams = $request->getServerParams();
return $serverParams['remote_addr'] ?? '';
}