【问题标题】:how to compress(gzip) body of a request in guzzle 6如何在 guzzle 6 中压缩(gzip)请求正文
【发布时间】:2016-08-11 03:04:50
【问题描述】:

需要将压缩 (gzip) 正文发送到服务器

例如

    protected function postOrPutData($method, $data, $type, $uri = null, array $options = [])
    {
        $requestBody = $this->serializer->serialize($data, 'json');

        $request  = new Request($method, $uri, [], $requestBody);
        $response = $this->httpClient->send($request, $options);

        return $this->serializer->deserialize((string) $response->getBody(), $type, 'json');
    }

【问题讨论】:

    标签: php guzzle guzzle6


    【解决方案1】:

    我想你也可以从similar question about pure cURL获得灵感。

    尝试将此自定义设置与 Guzzle 一起使用(我假设您将其与 cURL 处理程序一起使用):

    $options['curl'] = [CURLOPT_ENCODING => 'gzip'];
    
    $request  = new Request($method, $uri, [], $requestBody);
    $response = $this->httpClient->send($request, $options);
    

    【讨论】:

    • php 文档说 CURLOPT_ENCODING 为 response 而不是请求启用 gzip。
    • @Stalinko,你是对的,答案是错误的。标准的gzcompress 函数应该可以完成这项工作。
    • 正确,但这还不够。您还必须添加Content-Encoding: gzip
    • @Stalinko ,您能添加示例,您是如何解决这个问题的。还想将正文中的压缩 json 数据发送到服务器。但在 Guzzle 6 中找不到如何操作。
    猜你喜欢
    • 2012-08-22
    • 2023-04-10
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2018-09-21
    • 2011-05-21
    相关资源
    最近更新 更多