【问题标题】:GuzzleHttp send compress request with gzipGuzzleHttp 使用 gzip 发送压缩请求
【发布时间】:2015-09-22 15:05:27
【问题描述】:

我使用带有 API REST 的 GuzzleHttp。我的参数在

$url_complete = 'http://apirest.com/?'.$params;
$request = $client->post(
    $url_complete,
);

当我搜索解决方案时,我只得到 EntityBody 对象的解决方案 (http://guzzle3.readthedocs.org/http-client/entity-bodies.html)。但是 EntityBody 是 API 的响应。我不需要读取压缩数据,我必须发送它。

您知道使用 GuzzleHttp 将压缩(使用 gzip)数据发送到 API REST 的方法吗?

【问题讨论】:

  • 您是否仅限于使用 Guzzle 3 或者您是否有能力升级到 Guzzle 6?
  • 是的,由于系统管理员的限制,我不能使用 PHP 5.5。

标签: php rest guzzle


【解决方案1】:

引用参考文献#1:

实体主体是用于 HTTP 消息主体的术语。请求和响应的实体主体本质上是 Guzzle 中的 PHP 流。

这意味着 EntityBody 对象可以用于 HTTP 请求和响应。

Client::post() 的方法签名是post($uri = null, $headers = null, $postBody = null, array $options = array()). 在您的代码 sn-p 中可以看到,您没有设置发布请求的正文。事实上,你所做的只是设置一些 uri 查询参数。

根据 Ref #2、#3 和 #4:您可能想要执行以下操作:

$body = EntityBody::factory(fopen($file_location));
$body->compress(); //compresses the body using the deflate php stream filter

$request = $client->post($uri, $headers, $body, $options);

$response = $client->send($request);

我应该声明我从未真正这样做过,在过去 1.5 年的大部分时间里我一直在使用 v4+,但我目前无法对此进行测试。

参考资料:

  1. Entity Bodies
  2. Entity Bodies - Compression
  3. Using Request Objects - POST Requests
  4. Guzzle 3 - Client.php

【讨论】:

    猜你喜欢
    • 2016-02-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2021-06-11
    • 1970-01-01
    • 2011-01-22
    • 2010-11-29
    相关资源
    最近更新 更多