我收到来自第三方的 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
My curl:
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() 处理压缩(未 gzip 压缩)字符串,gzuncompress() 处理压缩(未 gzip 压缩)字符串。
Docs:
- 津弗拉特 https://www.php.net/manual/en/function.gzinflate.php
- 压缩包 https://www.php.net/manual/en/function.gzuncompress.php
- gzdecode https://www.php.net/manual/en/function.gzdecode.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)