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

});
jagershark
  • 1,162
  • 3
  • 15
  • 27
  • Possible duplicate of [Warning about \`$HTTP\_RAW\_POST\_DATA\` being deprecated](http://stackoverflow.com/questions/26261001/warning-about-http-raw-post-data-being-deprecated) – kenorb Jan 15 '16 at 19:17

3 Answers3

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.

Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
  • 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. – jagershark Feb 03 '15 at 10:37
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.

HILARUDEEN S ALLAUDEEN
  • 1,722
  • 1
  • 18
  • 33
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

Tuim
  • 2,491
  • 1
  • 14
  • 31
  • Unfortunately the error persists even using SLIM's request object. It actually occurs despite the route's callback function being empty. See my edit. – jagershark Feb 02 '15 at 14:46