php always_populate_raw_post_data - 无法从 Backbone create 访问请求负载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28278705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
always_populate_raw_post_data - Trouble accessing request payload from Backbone create
提问by loxyboi
I am trying to save a collection to my database RESTfully using Backbone.js with the SLIM php framework running on my server.
我正在尝试使用 Backbone.js 和在我的服务器上运行的 SLIM php 框架以 REST 方式将集合保存到我的数据库中。
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:
这是我的 SLIM 路线:
$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:
在调用create()
我的集合时,我在服务器的响应中收到了弃用警告:
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
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:
在我的 POST 路由中,我尝试接收请求有效负载,如下所示:
$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
});
回答by Mika Tuupola
There is a bug in PHP 5.6. Default value of always_populate_raw_post_data
is 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.
PHP 5.6 中存在一个错误。的默认值always_populate_raw_post_data
就是0
。这会导致 PHP 抛出警告,即使您的代码不使用$HTTP_RAW_POST_DATA
. 有些人声称header()
在输出某些文本后调用时会发生这种情况。尝试使用ini_set()
无济于事。
You must change the config directly in php.ini instead.
您必须直接在 php.ini 中更改配置。
always_populate_raw_post_data = -1
Related discussionin PHP internals.
PHP 内部的相关讨论。
回答by HILARUDEEN S ALLAUDEEN
Basically you can resolve Automatically populating $HTTP_RAW_POST_DATA is deprecated...
error in couple of ways,
基本上你可以Automatically populating $HTTP_RAW_POST_DATA is deprecated...
通过几种方式解决错误,
- PHP SETTINGS
- PHP 设置
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.
将 always_populate_raw_post_data 更改为 -1 php.ini 文件将解决该问题。但是,当您没有足够的控制 php.ini 文件时,就会出现问题。您可以考虑共享主机。
- 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.
将 .htaccess 文件内部更改为您的应用程序目录。这将仅对您的应用程序进行隔离控制。它不会影响其他应用程序执行的 APACHE 和 PHP。
<IfModule mod_php5.c>
php_value always_populate_raw_post_data -1
</IfModule>
<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.
我会推荐第二种方法。因为它允许您将应用程序放置在共享托管和专用服务器托管中。
回答by Tuim
You can request the POST data via the request object that comes from Slim.
您可以通过来自 Slim 的请求对象来请求 POST 数据。
$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