php-extended/php-http-client-gzip

符合psr-18规范的中间件客户端,处理gz压缩和解压缩

7.0.6 2024-07-31 13:47 UTC

README

符合psr-18规范的中间件客户端,处理gz压缩和解压缩。

coverage build status

安装

此库的安装通过composer完成,所有类的自动加载都通过其自动加载器完成。

  • 他们的网站下载 composer.phar
  • 然后运行以下命令安装此库作为依赖项
  • php composer.phar php-extended/php-http-client-gzip ^7

基本用法

此库用于为http请求和响应创建中间人,并在请求通过时记录事件。可以使用以下方式使用


/** @var $client Psr\Http\Message\ProcessorInterface */ // psr-18
/** @var $request Psr\Http\Message\RequestInterface */  // psr-7

$client = new GzipClient($client);
$response = $client->sendRequest($request);

/** @var $response Psr\Http\Message\ResponseInterface */

此库处理请求中添加 Accept-Header 和响应中根据 Content-Encoding 头解码。

此库还可以解压缩接收到的类似gz的文件。为了使其工作

  • 首先,请求必须包含一个指向有效文件路径的 X-Php-Download-File 头。如果该位置已存在文件,它将被覆盖。
  • 其次,http客户端必须支持 X-Php-Download-File 头以将下载的文件写入该位置并从该文件创建一个 StreamInterface

如果响应给 X-Php-Download-File 的文件是兼容gzip的文件,它将被解压缩到与当前文件相同的目录中,但具有一个新名字。tarball或等效文件中的所有文件都将连接到该位置。

此客户端将设置 X-Php-Uncompressed-File 头,以包含新解压缩文件的路径,并将指向下载文件的现有 StreamInterface 替换为指向解压缩文件的新 StreamInterface

下载的文件不会删除,删除下载的文件和解压缩文件是调用库的职责,一旦工作完成。


$client = new GzipClient($client);
$request = $request->withAddedHeader('X-Php-Download-File' => '/tmp/file.gz');
$response = $client->sendRequest($response);
$stream = $response->getBody();

/** @var $stream \Psr\Http\Message\StreamInterface */
// the stream points to the /tmp/{$sha1} file that is unzipped
// the exact location of the file can be uncovered with :
$filePath = $response->getHeader('X-Php-Uncompressed-File');

许可证

MIT (见 许可证文件).