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

How to decompress a gzip request PHP/Lumen/Laravel?

我收到来自第三方的请求,这些请求是 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 请求?

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

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

这是内置于 PHP。

gzinflate() 处理压缩(非 gzip 压缩)字符串和 gzuncompress() 压缩(非 gzip 压缩)字符串。

文档: