PHP 语言 使用PSR 45标准实现HTTP消息处理器

PHP阿木 发布于 2025-07-01 19 次阅读


摘要:

随着互联网技术的不断发展,HTTP协议已成为现代网络通信的基础。PHP作为一门流行的服务器端脚本语言,在处理HTTP请求和响应方面有着广泛的应用。PSR-45标准是PHP社区为了提高代码质量和可维护性而制定的一个规范。本文将围绕PSR-45标准,探讨如何在PHP中实现一个高效的HTTP消息处理器。

一、

PSR-45标准,全称为“HTTP Message Implementation”,是PHP-FIG(PHP Framework Interop Group)制定的一个规范。该规范旨在提供一个统一的接口,用于处理HTTP请求和响应。通过遵循PSR-45标准,可以确保不同框架和库之间的HTTP消息处理具有一致性,从而提高代码的可维护性和可扩展性。

二、PSR-45标准概述

PSR-45标准定义了以下接口:

1. RequestInterface:表示HTTP请求。

2. ResponseInterface:表示HTTP响应。

3. ServerRequestInterface:扩展了RequestInterface,增加了服务器端特有的信息。

4. ResponseInterface:扩展了ResponseInterface,增加了服务器端特有的信息。

三、实现HTTP消息处理器

下面将基于PSR-45标准,使用PHP实现一个简单的HTTP消息处理器。

1. 创建Request类

php

<?php


namespace HttpMessage;

use PsrHttpMessageRequestInterface;

class Request implements RequestInterface


{


private $method;


private $uri;


private $headers;


private $body;

public function __construct($method, $uri, $headers, $body)


{


$this->method = $method;


$this->uri = $uri;


$this->headers = $headers;


$this->body = $body;


}

public function getMethod(): string


{


return $this->method;


}

public function getUri(): UriInterface


{


// 实现UriInterface


}

public function getHeader(string $name): array


{


return $this->headers[$name] ?? [];


}

public function getHeaders(): array


{


return $this->headers;


}

public function getHeaderLine(string $name): string


{


// 实现getHeaderLine


}

public function getBody(): BodyInterface


{


// 实现BodyInterface


}

public function withMethod($method): RequestInterface


{


// 实现withMethod


}

public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface


{


// 实现withUri


}

public function withHeader(string $name, $value): RequestInterface


{


// 实现withHeader


}

public function withHeaders(array $headers): RequestInterface


{


// 实现withHeaders


}

public function withBody($body): RequestInterface


{


// 实现withBody


}


}


2. 创建Response类

php

<?php


namespace HttpMessage;

use PsrHttpMessageResponseInterface;

class Response implements ResponseInterface


{


private $statusCode;


private $headers;


private $body;

public function __construct($statusCode, $headers, $body)


{


$this->statusCode = $statusCode;


$this->headers = $headers;


$this->body = $body;


}

public function getStatusCode(): int


{


return $this->statusCode;


}

public function getReasonPhrase(): string


{


// 实现getReasonPhrase


}

public function getHeader(string $name): array


{


return $this->headers[$name] ?? [];


}

public function getHeaders(): array


{


return $this->headers;


}

public function getHeaderLine(string $name): string


{


// 实现getHeaderLine


}

public function withStatus($statusCode): ResponseInterface


{


// 实现withStatus


}

public function withHeader(string $name, $value): ResponseInterface


{


// 实现withHeader


}

public function withHeaders(array $headers): ResponseInterface


{


// 实现withHeaders


}

public function withBody($body): ResponseInterface


{


// 实现withBody


}


}


3. 创建ServerRequest类

php

<?php


namespace HttpMessage;

use PsrHttpMessageServerRequestInterface;

class ServerRequest extends Request implements ServerRequestInterface


{


private $serverParams;


private $queryParams;


private $cookieParams;


private $uploadedFiles;


private $attributes;

public function __construct($method, $uri, $headers, $body, $serverParams, $queryParams, $cookieParams, $uploadedFiles, $attributes)


{


parent::__construct($method, $uri, $headers, $body);


$this->serverParams = $serverParams;


$this->queryParams = $queryParams;


$this->cookieParams = $cookieParams;


$this->uploadedFiles = $uploadedFiles;


$this->attributes = $attributes;


}

public function getServerParams(): array


{


return $this->serverParams;


}

public function getQueryParams(): array


{


return $this->queryParams;


}

public function getCookieParams(): array


{


return $this->cookieParams;


}

public function getUploadedFiles(): array


{


return $this->uploadedFiles;


}

public function getAttributes(): array


{


return $this->attributes;


}

public function withServerParams(array $serverParams): ServerRequestInterface


{


// 实现withServerParams


}

public function withQueryParams(array $queryParams): ServerRequestInterface


{


// 实现withQueryParams


}

public function withCookieParams(array $cookieParams): ServerRequestInterface


{


// 实现withCookieParams


}

public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface


{


// 实现withUploadedFiles


}

public function withAttributes(array $attributes): ServerRequestInterface


{


// 实现withAttributes


}


}


4. 创建ResponseInterface实现类

php

<?php


namespace HttpMessage;

use PsrHttpMessageResponseInterface;

class ResponseImpl implements ResponseInterface


{


private $statusCode;


private $headers;


private $body;

public function __construct($statusCode, $headers, $body)


{


$this->statusCode = $statusCode;


$this->headers = $headers;


$this->body = $body;


}

public function getStatusCode(): int


{


return $this->statusCode;


}

public function getReasonPhrase(): string


{


// 实现getReasonPhrase


}

public function getHeader(string $name): array


{


return $this->headers[$name] ?? [];


}

public function getHeaders(): array


{


return $this->headers;


}

public function getHeaderLine(string $name): string


{


// 实现getHeaderLine


}

public function withStatus($statusCode): ResponseInterface


{


// 实现withStatus


}

public function withHeader(string $name, $value): ResponseInterface


{


// 实现withHeader


}

public function withHeaders(array $headers): ResponseInterface


{


// 实现withHeaders


}

public function withBody($body): ResponseInterface


{


// 实现withBody


}


}


四、总结

本文基于PSR-45标准,实现了PHP中的HTTP消息处理器。通过遵循PSR-45规范,我们可以提高代码的可维护性和可扩展性。在实际项目中,可以根据需求对上述实现进行扩展和优化,以满足不同的业务场景。

注意:本文仅为示例,实际实现中可能需要考虑更多的细节和异常处理。