从使用 Transfer-Encoding: chunked 的请求中读取 php://input

问题描述

在我漫长的职业生涯中,我第一次在请求使用 transfer-encoding: chunked(带有 Content-Type: application/json)时努力检索 RESTful 事件通知的输入正文

$_POST 显然是空的,因为 Content-Type: application/json

file_get_contents('PHP://input') 总是空的,希望有人能解释一下; Apache 没有将其传递给 mod_PHP 吗?

尝试使用 fopen 检索块也会失败?

$hSource = fopen('PHP://input','r');
$body = '';

while (!feof($hSource)) {
    $chunk = fread($hSource,1024);
    $body .= $chunk;
}
fclose($hSource);

这是一个死胡同还是我完全错了?我知道,如果我让服务添加 Content-Length 标头,那么上述所有内容实际上都可以正常工作,但他们强调,这项 5 分钟的任务需要几个月的时间才能完成。

Apache downgrade-1.0 (ew...) 也不会强制他们将请求作为 HTTP/1.0 发送。

谢谢

解决方法

在我的场景中,这是由于@Phil 在评论中指出的错误

这会特别影响 php-fpm,对于大多数用户,他们可以将 Apache 升级到 2.4.46+。

错误: https://bz.apache.org/bugzilla/show_bug.cgi?id=57087

如果您是 Debian 用户,在撰写本文时; 2.4.46 还没有正式的稳定版本,它正处于测试阶段,如图 here。我们在等待稳定版本的同时退回到 mod_php,其他人可能更倾向于使用 Nginx。