Error $ HTTP_RAW_POST_DATA is deprecated

6

After upgrading PHP to version 5.6, I started to receive 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 on line 0

Is there any alternative to using $HTTP_RAW_POST_DATA ?

    
asked by anonymous 17.02.2015 / 14:46

1 answer

7

The $HTTP_RAW_POST_DATA is deprecated as of version 5.6 bolded , which causes warning as described in the documentation:

This message specifies Automatically populating $HTTP_RAW_POST_DATA occurs when php.ini is set 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 does not exist anymore, 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 using $HTTP_RAW_POST_DATA you can use wrappers , in the case to get the input data we use php://input

A simple example:

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

If the data is very large (extensive) you can do a "loop" for reading, example of how to write the data to 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 warnings and still use $HTTP_RAW_POST_DATA , but as of PHP7 it does not work any more, in> to maintain compatibility.

    
17.02.2015 / 14:46