jumbodroid / httpclient
HttpClient 是一个用于 PHP 和 Laravel 的 HTTP 客户端实现
Requires
- php: >=7.2.5
- ext-curl: *
- psr/http-client: >=1.0
- psr/http-factory: >=1.0
- psr/http-message: >=2.0
- symfony/options-resolver: >=5.1
Requires (Dev)
- mockery/mockery: >=1.3.1
- phpunit/phpunit: >=8.5
- symfony/var-dumper: >=5.2.0
Provides
This package is auto-updated.
Last update: 2024-09-09 13:42:24 UTC
README
httpclient 是一个 PHP & Laravel 包,旨在帮助 PHP 开发者构建健壮的现代应用程序,这些应用程序能够与后端的其他应用程序和 API 交互,从而为用户提供令人喜爱的体验。
httpclient 遵循或实现了以下 PHP 标准建议(PSRs)
利用 httpclient 构建 Web 的现代互联应用程序。
httpclient 可以让您免受处理创建互联 Web 应用程序所需的更平凡和更低级 API 的困扰。
安装
composer require jumbodroid/httpclient
使用方法
PSR-7
PSR-7 描述了表示 HTTP 消息的常见接口,如 RFC 7230 和 RFC 7231 所述,以及用于与 HTTP 消息一起使用的 URI,如 RFC 3986 所述。本软件包包括 PSR-7 的实现。
请求 | 响应
使用 getHeaderLine()
方法获取一个字符串形式的头部值,包含所有不区分大小写的头部值,通过逗号连接。
$response->withHeader('foo', 'bar') ->withAddedHeader('foo', 'baz'); $response->getHeaderLine('foo'); // outputs 'bar,baz'
使用 getHeader()
获取一个特定不区分大小写的头部名称的所有头部值的数组。
$response->getHeader('foo'); // outputs ['bar', 'baz']
注意:并非所有头部值都可以使用逗号连接(例如,Set-Cookie
)。当处理此类头部时,请求和响应类的消费者应依靠 getHeader()
方法来检索此类多值头部。
请求
$request->getProtocolVersion(); $request->withProtocolVersion($version); $request->getHeaders(); $request->hasHeader($name); $request->getHeader($name); $request->getHeaderLine($name); $request->withHeader($name, $value); $request->withAddedHeader($name, $value); $request->withoutHeader($name); $request->getBody(); $request->withBody(StreamInterface $body);
出站,客户端请求
$request->getRequestTarget(); $request->withRequestTarget($requestTarget); $request->getMethod(); $request->withMethod($method); $request->getUri(); $request->withUri(UriInterface $uri, $preserveHost = false);
入站,服务器端请求
$request->getServerParams(); $request->getCookieParams(); $request->withCookieParams(array $cookies); $request->getQueryParams(); $request->withQueryParams(array $query); $request->getUploadedFiles(); $request->withUploadedFiles(array $uploadedFiles); $request->getParsedBody(); $request->withParsedBody($data); $request->getAttributes(); $request->getAttribute($name, $default = null); $request->withAttribute($name, $value); $request->withoutAttribute($name);
响应
$response->getProtocolVersion(); $response->withProtocolVersion($version); $response->getHeaders(); $response->hasHeader($name); $response->getHeader($name); $response->getHeaderLine($name); $response->withHeader($name, $value); $response->withAddedHeader($name, $value); $response->withoutHeader($name); $response->getBody(); $response->withBody(StreamInterface $body);
出站,服务器端响应
$response->getStatusCode(); $response->withStatus($code, $reasonPhrase = ''); $response->getReasonPhrase();
流
$stream->__toString(); $stream->close(); $stream->detach(); $stream->getSize(); $stream->tell(); $stream->eof(); $stream->isSeekable(); $stream->seek($offset, $whence = SEEK_SET); $stream->rewind(); $stream->isWritable(); $stream->write($string); $stream->isReadable(); $stream->read($length); $stream->getContents(); $stream->getMetadata($key = null);
URI
$uri->getScheme(); $uri->getAuthority(); $uri->getUserInfo(); $uri->getHost(); $uri->getPort(); $uri->getPath(); $uri->getQuery(); $uri->getFragment(); $uri->withScheme($scheme); $uri->withUserInfo($user, $password = null); $uri->withHost($host); $uri->withPort($port); $uri->withPath($path); $uri->withQuery($query); $uri->withFragment($fragment); $uri->__toString();
上传文件
$uploadedFile->getStream(); $uploadedFile->moveTo($targetPath); $uploadedFile->getSize(); $uploadedFile->getError(); $uploadedFile->getClientFilename(); $uploadedFile->getClientMediaType();
PSR-17
PSR-17 描述了一个用于创建 PSR-7 兼容 HTTP 对象的常见标准。本软件包提供了 PSR-17 的实现。
RequestFactory
RequestFactory::createRequest(string $method, $uri);
ResponseFactory
ResponseFactory::createResponse(int $code = 200, string $reasonPhrase = '');
ServerRequestFactory
ServerRequestFactory::createServerRequest(string $method, $uri, array $serverParams = []);
StreamFactory
StreamFactory::createStream(string $content = ''); StreamFactory::createStreamFromFile(string $filename, string $mode = 'r'); StreamFactory::createStreamFromResource($resource);
UploadedFileFactory
UploadedFileFactory::createUploadedFile( StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null );
UriFactory
UriFactory::createUri(string $uri = '');
PSR-18
客户端 - 客户端是一个库,它实现了本规范,用于发送 PSR-7 兼容的 HTTP 请求消息并返回一个 PSR-7 兼容的 HTTP 响应消息给调用库。
调用库 - 调用库是任何使用客户端的代码。它不实现本规范的接口,但消耗实现它们的对象(客户端)。
客户端
$client->sendRequest(RequestInterface $request);
ClientException
RequestException
$requestException->getRequest();
NetworkException
$requestException->getRequest();