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

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


摘要:

随着互联网技术的不断发展,HTTP协议已成为现代网络通信的基础。PHP作为一门流行的服务器端脚本语言,在处理HTTP请求和响应方面有着广泛的应用。PSR-147标准为PHP HTTP消息处理器提供了规范,本文将围绕这一标准,探讨如何在PHP中实现一个符合PSR-147的HTTP消息处理器。

一、

PSR-147标准是PHP框架互操作性小组(PHP Framework Interop Group,简称PHP-FIG)制定的一个规范,旨在统一PHP框架中HTTP消息处理器的接口。该标准定义了HTTP请求和响应的基本结构,以及处理这些消息的方法。遵循PSR-147标准,可以确保不同框架之间的HTTP消息处理器具有良好的兼容性。

二、PSR-147标准概述

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

1. RequestInterface:表示HTTP请求。

2. ResponseInterface:表示HTTP响应。

3. ServerRequestInterface:扩展了RequestInterface,增加了服务器相关的信息。

4. ResponseInterface:扩展了ResponseInterface,增加了响应相关的信息。

5. ServerRequestFactoryInterface:用于创建ServerRequest对象。

6. ResponseFactoryInterface:用于创建Response对象。

三、实现HTTP消息处理器

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

1. 定义RequestInterface和ResponseInterface

php

interface RequestInterface


{


public function getMethod(): string;


public function getUri(): UriInterface;


public function getServerParams(): array;


public function getQueryParams(): array;


public function getCookieParams(): array;


public function getHeaders(): array;


public function getBody(): string;


}

interface ResponseInterface


{


public function getStatusCode(): int;


public function getReasonPhrase(): string;


public function getHeaderLine(string $name): string;


public function getHeaders(): array;


public function getBody(): string;


public function withStatus(int $code, string $reasonPhrase = 'OK'): ResponseInterface;


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


public function withBody(string $body, $headers = []): ResponseInterface;


}


2. 实现ServerRequestInterface和ResponseInterface

php

class ServerRequest implements ServerRequestInterface


{


private $method;


private $uri;


private $serverParams;


private $queryParams;


private $cookieParams;


private $headers;


private $body;

public function __construct(string $method, UriInterface $uri, array $serverParams, array $queryParams, array $cookieParams, array $headers, string $body)


{


$this->method = $method;


$this->uri = $uri;


$this->serverParams = $serverParams;


$this->queryParams = $queryParams;


$this->cookieParams = $cookieParams;


$this->headers = $headers;


$this->body = $body;


}

public function getMethod(): string


{


return $this->method;


}

public function getUri(): UriInterface


{


return $this->uri;


}

public function getServerParams(): array


{


return $this->serverParams;


}

public function getQueryParams(): array


{


return $this->queryParams;


}

public function getCookieParams(): array


{


return $this->cookieParams;


}

public function getHeaders(): array


{


return $this->headers;


}

public function getBody(): string


{


return $this->body;


}


}

class Response implements ResponseInterface


{


private $statusCode;


private $reasonPhrase;


private $headers;


private $body;

public function __construct(int $statusCode, string $reasonPhrase, array $headers, string $body)


{


$this->statusCode = $statusCode;


$this->reasonPhrase = $reasonPhrase;


$this->headers = $headers;


$this->body = $body;


}

public function getStatusCode(): int


{


return $this->statusCode;


}

public function getReasonPhrase(): string


{


return $this->reasonPhrase;


}

public function getHeaderLine(string $name): string


{


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


}

public function getHeaders(): array


{


return $this->headers;


}

public function getBody(): string


{


return $this->body;


}

public function withStatus(int $code, string $reasonPhrase = 'OK'): ResponseInterface


{


$newResponse = new self($code, $reasonPhrase, $this->headers, $this->body);


return $newResponse;


}

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


{


$newHeaders = $this->headers;


$newHeaders[$name] = $value;


$newResponse = new self($this->statusCode, $this->reasonPhrase, $newHeaders, $this->body);


return $newResponse;


}

public function withBody(string $body, $headers = []): ResponseInterface


{


$newResponse = new self($this->statusCode, $this->reasonPhrase, $this->headers, $body);


return $newResponse;


}


}


3. 创建ServerRequestFactoryInterface和ResponseFactoryInterface

php

interface ServerRequestFactoryInterface


{


public function createServerRequest(array $serverParams, array $queryParams, array $cookieParams, array $headers, string $body);


}

interface ResponseFactoryInterface


{


public function createResponse(int $statusCode, string $reasonPhrase, array $headers, string $body);


}

class DefaultServerRequestFactory implements ServerRequestFactoryInterface


{


public function createServerRequest(array $serverParams, array $queryParams, array $cookieParams, array $headers, string $body)


{


$uri = new Uri();


return new ServerRequest('GET', $uri, $serverParams, $queryParams, $cookieParams, $headers, $body);


}


}

class DefaultResponseFactory implements ResponseFactoryInterface


{


public function createResponse(int $statusCode, string $reasonPhrase, array $headers, string $body)


{


return new Response($statusCode, $reasonPhrase, $headers, $body);


}


}


四、总结

本文介绍了PSR-147标准,并使用PHP实现了符合该标准的HTTP消息处理器。通过遵循PSR-147标准,可以确保不同框架之间的HTTP消息处理器具有良好的兼容性,从而提高开发效率和代码质量。

在实际应用中,可以根据具体需求对HTTP消息处理器进行扩展和优化。例如,可以添加中间件来处理请求和响应,或者实现自定义的请求和响应工厂。遵循PSR-147标准,可以让我们在PHP中构建更加健壮和可扩展的HTTP消息处理器。