Error $HTTP_RAW_POST_DATA is deprecated

Asked

Viewed 4,280 times

7

After upgrading PHP to version 5.6 I started receiving the following message:

Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be Removed in a Future version. To avoid this Warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream Instead. in Unknown online 0

Is there any alternative to using the $HTTP_RAW_POST_DATA?

1 answer

11


The $HTTP_RAW_POST_DATA is out of use from version 5.6 underlined text, what causes the Warning, as described in the documentation:

This message specifies Automatically populating $HTTP_RAW_POST_DATA occurs when the php.ini is configured to automatically generate the variable $HTTP_RAW_POST_DATA through the flag always_populate_raw_post_data.

The always_populate_raw_post_data by default is On in older versions of PHP, but in PHP7 it no longer exists, so the problem will not occur if your PHP is an older version, so just set up php.ini like this:

always_populate_raw_post_data=-1

Save and restart Apache/Ngnix/Lighttpd.

As an alternative to the use of $HTTP_RAW_POST_DATA one can use the wrappers, in case to take the input data we use php://input

A simple example:

<?php
$postdata = file_get_contents('php://input');

If the data is too large (extensive) you can make a "loop" for reading, example of how to write the data in a file:

<?php
$fileHandle  = fopen('arquivo.txt', 'wb');
$inputHandle = fopen('php://input', 'rb');

if ($fileHandle && $inputHandle) {
    while(FALSE === feof($inputHandle)) {
        $data = fgets($inputHandle, 256);
        fwrite($fileHandle, $data);
    }

    fclose($fileHandle);
    fclose($inputHandle);
}

Note that you can turn off the warnings and still use $HTTP_RAW_POST_DATA, but from PHP7 it no longer works, as said before, so prefer from now use Wrappers to maintain compatibility.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.