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

时间:2019-03-27 13:38:46

标签: php laravel-5 http-post gzip lumen

我正在接收来自第三方的gzip编码请求(〜1mb,这样才有意义)

我的考试路线:

$router->post(
    'testgzip',
    function (\Illuminate\Http\Request $request) {
        $decompressed = null;
        if ($request->header('content-encoding') === 'gzip') {
            $decompressed = gzinflate($request->getContent());
        }

        return [
            'body' => $decompressed ?? $request->getContent(),
        ];
    }
);

我的测试文件 test.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请求?

1 个答案:

答案 0 :(得分:2)

对于压缩后的内容,您需要使用gzdecode()

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

这是PHP内置的。

gzinflate()处理压缩(不压缩)的gdef和gzuncompress()处理压缩(不压缩)的字符串。

文档: