always_populate_raw_post_data - Trouble accessing request payload from Backbone create
Asked Answered
P

3

7

I am trying to save a collection to my database RESTfully using Backbone.js with the SLIM php framework running on my server.

Here is my collection:

var newUser = this.collection.create(
    formData,
    {
        wait: true,
        success: $.proxy(function() {
            this.collection.currentUser = newUser;
            App.Router.navigate('', { trigger: true });
        }, this)
    }
);

Here is my SLIM route:

$api->post('/users', function() use($api, $db) {

    $request = $api->request()->post();

    $api->response()->header('Content-Type', 'application/json');

    $result = $db->users()->insert($user);

    if( $result ) {
        echo json_encode(array(
            'id' => $result['id']
        ));
    }
    else {
        echo json_encode(array(
            'status' => false,
            'message' => 'error_creating_user'
        ));
    }

});

$api->run();

When calling create() on my collection, I get a deprecation warning in the server's response:

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

I have followed these instructions and done the following:

I have added this before my routes:

ini_set('always_populate_raw_post_data', '-1');

and from within my POST route I have tried to receive the request payload like so:

$request = file_get_contents('php://input');

After this change to my code, the response I am getting has remained the same...

EDIT

The error occurs even with an empty callback....

$api->post('/users', function() use($api, $db) {

    // nothing

});
Patrica answered 2/2, 2015 at 13:21 Comment(1)
Possible duplicate of Warning about `$HTTP_RAW_POST_DATA` being deprecatedPlay
A
24

There is a bug in PHP 5.6. Default value of always_populate_raw_post_datais 0. This causes PHP to throw warnings even if your code does not use $HTTP_RAW_POST_DATA. Some claim it happens when calling header() after some text has already been outputted. Trying to use ini_set()does not help.

You must change the config directly in php.ini instead.

always_populate_raw_post_data = -1

Related discussion in PHP internals.

Appeal answered 3/2, 2015 at 5:2 Comment(1)
Thank you. It appears to have been this problem. For now, as my code does not specifically need 5.6, I have swapped version 5.6 back down to 5.5 and the problem is gone.Patrica
S
5

Basically you can resolve Automatically populating $HTTP_RAW_POST_DATA is deprecated... error in couple of ways,

  1. PHP SETTINGS

Changing always_populate_raw_post_data to -1 php.ini file will resolve the issue. However, it becomes the problem where you don't have enough control the php.ini file. You can think of shared hosting.

  1. APACHE SETTINGS

Changing .htaccess file inside to your application directory. This will give isolated control over your application only. It will affect neither APACHE nor PHP of other application execution.

<IfModule mod_php5.c> php_value always_populate_raw_post_data -1 </IfModule>

And I would recommend the second approach. Since it allows you to place your application in both shared hosting and dedicated server hosting.

Soldo answered 15/7, 2016 at 7:13 Comment(1)
Yes, you right! The second approach is more portableCarbonaceous
B
0

You can request the POST data via the request object that comes from Slim.

$api->post('/users', function() use ($api) {
    var_dump($api->request()->post());
    var_dump($api->request()->post('specificKey'));
});

Here is the documentation: http://docs.slimframework.com/#Request-Variables

Borlase answered 2/2, 2015 at 13:34 Comment(1)
Unfortunately the error persists even using SLIM's request object. It actually occurs despite the route's callback function being empty. See my edit.Patrica

© 2022 - 2024 — McMap. All rights reserved.