GuzzleHttp用gzip发送压缩请求

时间:2015-09-22 15:05:28

标签: php rest guzzle

我使用GuzzleHttp和API REST。我的参数在

$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的方法?

1 个答案:

答案 0 :(得分:0)

引用参考文献1:

  

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

这意味着EntityBody对象可以与HTTP请求和响应一起使用。

Client::post()的方法签名是post($uri = null, $headers = null, $postBody = null, array $options = array()).正如您的代码段中所示,您没有设置帖子请求的正文。事实上,您所做的只是设置一些uri查询参数。

基于参考#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
相关问题