qa-data / psr7-http-message
HTTP 消息接口
v1.0
2024-10-01 13:55 UTC
Requires
- php: >=8.1
- guzzlehttp/psr7: ^2.7
README
PSR-7 是 HTTP 消息接口。它是一个非常有用的接口,尤其是在中间件/中继模式中。更多内容请访问官方 PHP-FIG 网站。
本包基于 Contributte 团队的一个包 - PSR-7 HTTP Message,该包基于 guzzle/psr7。
使用方法
要安装最新版本的 qa-data/psr7-http-message,请使用 Composer。
composer require qa-data/psr7-http-message
Psr7Request
最简单的方法是使用 Psr7RequestFactory 创建请求。
use Contributte\Psr7\Psr7RequestFactory; $psr7 = Psr7RequestFactory::fromGlobal();
其他方法(针对 PSR7 接口)
- of(RequestInterface $request): self
- getContents(): mixed
- getContentsCopy(): mixed
- getJsonBody(bool $associative = true): mixed
- getJsonBodyCopy(bool $associative = true): mixed
- withNewUri(string $uri): self - 返回具有给定 URL 的克隆
Psr7ServerRequest
其他方法(针对 PSR7 接口)
- normalizeNetteFiles(Nette\Http\FileUpload[] $files): Psr7UploadedFile[]
- of(ServerRequestInterface $request): self
- fromGlobals(): self
- withAttributes(array $attributes): self
- hasQueryParam(string $name): bool
- getQueryParam(string $name, mixed $default = null): mixed
Psr7Response
最简单的方法是使用 Psr7ResponseFactory 创建响应。
use Contributte\Psr7\Psr7ResponseFactory; $psr7 = Psr7ResponseFactory::fromGlobal();
其他方法(针对 PSR7 接口)
- of(ResponseInterface $response): self
- fromGlobals(): self
- appendBody(mixed $body): self
- rewindBody(): self
- writeBody(mixed $body): self
- writeJsonBody(array $data): self
- writeJsonObject(JsonSerializable $object): self
- getJsonBody(bool $associative = true): mixed
- getContents(bool $rewind = true): mixed
- withHeaders(array $headers): self
- getHttpResponse(): ?Nette\Http\IResponse
- withHttpResponse(Nette\Http\IResponse $response)
- hasHttpResponse(): bool
- getApplicationResponse(): ?Nette\Application\IResponse
- withApplicationResponse(Nette\Application\IResponse $response)
- hasApplicationResponse(): bool
- send(): void
- sendHeaders(): void
- sendBody(): void
API
MessageInterface
getProtocolVersion: stringwithProtocolVersion(string $version): staticgetHeaders(): arrayhasHeader(string $name): boolgetHeader(string $name): string[]getHeaderLine(string $name): stringwithHeader(string $name, string|string[] $value): staticwithAddedHeader(string $name, string|string[] $value): staticwithoutHeader(string $name): staticgetBody(): StreamInterfacewithBody(StreamInterface $body): static
RequestInterface << MessageInterface
getRequestTarget(): stringwithRequestTarget($requestTarget): staticgetMethod(): stringwithMethod(string $method): staticgetUri(): UriInterfacewithUri(UriInterface $uri, bool $preserveHost = false): static
ServerRequestInterface << RequestInterface
getServerParams(): arraygetCookieParams(): arraywithCookieParams(array $cookies): staticgetQueryParams(): arraywithQueryParams(array $query): staticgetUploadedFiles(): UploadedFileInterface[]withUploadedFiles(array $uploadedFiles): staticgetParsedBody(): mixedwithParsedBody($data): staticgetAttributes(): mixed[]getAttribute(string $name, $default = null): mixedwithAttribute(string $name, $value): staticwithoutAttribute(string $name): static
ResponseInterface << MessageInterface
getStatusCode(): intwithStatus(int $code, string $reasonPhrase = ''): staticgetReasonPhrase(): string
StreamInterface
__toString(): stringclose(): voiddetach(): ?resourcegetSize(): ?inttell(): inteof(): boolisSeekable(): boolseek(int $offset, int $whence = SEEK_SET): voidrewind(): voidisWritable(): boolwrite($string): voidisReadable(): boolread($length): stringgetContents(): stringgetMetadata(?string $key = null): mixed
UriInterface
getScheme(): stringwithScheme(string $scheme): staticgetAuthority(): stringgetUserInfo(): stringwithUserInfo(string $user, ?string $password = null): staticgetHost(): stringwithHost(string $host): staticgetPort(): ?intwithPort(?int $port): staticgetPath(): stringwithPath(string $path): staticgetQuery(): stringwithQuery(string $query): staticgetFragment(): stringwithFragment(string $fragment): static__toString(): string
UploadedFileInterface
getStream(): StreamInterfacemoveTo($targetPath): voidgetSize(): ?intgetError(): intgetClientFilename(): ?stringgetClientMediaType(): ?string