代码
我在index.php
中有以下非常简单的代码:
<?php
if (!function_exists('getallheaders')) {
// Outputs all HTTP headers
function getallheaders()
{
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
echo "Content:\n" . @file_get_contents("php://input") . "\n" .
"Post:\n" . print_r($_POST, true) . "\n" .
"Headers:\n" . print_r(getallheaders(), true) . "\n";
die();
对照测试
我在本地运行服务器,如下所示:
$ php -S localhost:8080
然后我运行以下命令:
$ curl -H "Transfer-Encoding: chunked" -d "payload to send" http://localhost:8080
和它返回预期:
Content:
payload to send
Post:
Array
(
[payload_to_send] =>
)
Headers:
Array
(
[Host] => localhost:8080
[User-Agent] => curl/7.55.1
[Accept] => */*
[Transfer-Encoding] => chunked
[Content-Type] => application/x-www-form-urlencoded
)
在这里都好!
我还在PHP 7.0的Ubuntu 16.04上对此进行了测试,并且一切正常!
问题
每当运行PHP 7.1,7.2或7.3或更大的用Apache 2组合的服务器上运行相同的代码时,得到以下输出:
Content:
Post:
Array
(
)
Headers:
Array
(
[Transfer-Encoding] => chunked
[Accept] => */*
[User-Agent] => curl/7.55.1
[Host] => thedealerapp.test
)
请注意,如果我删除了传输编码标头,所以它没有被分块,则一切正常。显然,PHP和/或Apache存在一个问题,即不允许分块请求。
一个类似的问题被描述如下: POST request to PHP7 with chunked encoding does not properly return result
但是它被标记为已解决,并且我正在使用较新的PHP 7.1,PHP7.2和PHP 7.3。
为什么会发生这种情况,我该如何解决?非常感谢!
更新
我发现只有PHP的FPM版本会发生此问题。 PHP的普通版本似乎做工精细。
更新2
由于@IVO GELOV,我现在设法使其在nginx上运行。看来这是php-fpm和Apache组合的问题。