哪个函数可以缩小 http 请求?

Which function to deflate a http request?

我向远程服务发出 HTTP POST 请求,要求 post body 被“压缩”(并且 Content-encoding: deflate 应该在 headers).据我了解,RFC 1950 中涵盖了这一点。我应该使用哪个 php 函数才能兼容?

Content-Encoding: deflate 需要显示数据 using the zlib structure (defined in RFC 1950), with the deflate compression algorithm (defined in RFC 1951).

考虑

<?php
    $str = 'test';

    $defl = gzdeflate($str);
    echo bin2hex($defl), "\n";

    $comp = gzcompress($str);
    echo bin2hex($comp), "\n";
?>

这给了我们:

2b492d2e0100
789c2b492d2e0100045d01c1

所以 gzcompress 结果是 gzdeflate 前面有 789c 的缓冲区,这似乎是一个有效的 zlib header

0111     |  1000       |  11100   |  0        |  10
CINFO    |  CM         |  FCHECK  |  FDICT    |  FLEVEL
7=32bit  |  8=deflate  |          |  no dict  |  2=default algo

然后是 4 个字节的校验和。这就是我们要找的。

总而言之,

  • gzdeflate returns 原始压缩缓冲区 (RFC 1951)
  • gzcompress returns 一个用 zlib 东西包装的压缩缓冲区 (RFC 1950)
  • Content-Encoding: deflate需要wrapped buffer,即发送deflated数据时使用gzcompress.

注意混淆的命名:gzdeflate 而不是 对于 Content-Encoding: deflategzcompress 不是 对于 Content-Encoding: compress。去搞清楚!