如何解压缩 gzip 请求 PHP/Lumen/Laravel



我收到来自第三方的请求,这些请求是gzip编码的文本(~1mb,所以这是有道理的(

我的测试路线:

$router->post(
    'testgzip',
    function (IlluminateHttpRequest $request) {
        $decompressed = null;
        if ($request->header('content-encoding') === 'gzip') {
            $decompressed = gzinflate($request->getContent());
        }
        return [
            'body' => $decompressed ?? $request->getContent(),
        ];
    }
);

我的测试文件测试.txt

hello world!

我的健全性检查:

curl --data-binary @test.txt -H "Content-Type: text/plain" -X POST http://localhost:8000/testgzip 
{"body":"hello world!"}    

要压缩它,我运行命令 gzip test.txt

我的卷发:

curl --data-binary @test.txt.gz -H "Content-Type: text/plain" -H "Content-Encoding: gzip" -X POST http://localhost:8000/testgzip

这触发了

gzinflate((: 数据错误

我还尝试了触发的gzuncompress

gzuncompress((: 数据错误

我做错了什么?如何解压缩 gzip 请求?

对于 gzip 内容,您需要使用 gzdecode() .

$decompressed = gzdecode($request->getContent());

这是内置在 PHP 上的。

gzinflate(( 处理 deflated (not gzipped( 和 gzuncompress(( with compesed (not gzipped( 字符串。

文档:

  • Gzinfexpand
  • 尊压缩
  • GZDECODE

这个对我有用

gzuncompress(base64_decode($request->getContent()));

最新更新