PHP 7 FPM does not show request body with chunked encoding

故事扮演 提交于 2019-12-24 07:06:15

问题


The Code

I have the following very simple code in 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();

Control Test

I ran the server locally as follows:

$ php -S localhost:8080

I then ran the following command:

$ curl -H "Transfer-Encoding: chunked" -d "payload to send" http://localhost:8080

And it returned as expected:

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
)

All good here!

I also tested this on Ubuntu 16.04 on PHP 7.0, and it all works fine!

The Problem

Whenever I run the same code on a server running PHP 7.1, 7.2 or 7.3 or greater combined with Apache 2, I get the following output:

Content:

Post:
Array
(
)

Headers:
Array
(
    [Transfer-Encoding] => chunked
    [Accept] => */*
    [User-Agent] => curl/7.55.1
    [Host] => thedealerapp.test
)

Note that if I remove the transfer encoding header, so it is not chunked, everything works fine. So clearly there is an issue with PHP and/or apache that is not allowing chunked requests.

A similar issue is described here: POST request to PHP7 with chunked encoding does not properly return result

But it's marked as solved, and I'm using a newer PHP 7.1, PHP7.2 & PHP 7.3.

Why is this happening and how can I fix it? Many thanks!

Update

I have figured out that this issue occurs with only FPM versions of PHP. Normal versions of PHP seems to work fine.

Update 2

Thanks to @IVO GELOV I have now managed to get it to work on nginx. So it looks like it is a php-fpm & Apache combination issue.

来源:https://stackoverflow.com/questions/54444220/php-7-fpm-does-not-show-request-body-with-chunked-encoding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!