2

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.

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231

0 Answers0