【发布时间】:2014-08-08 20:51:37
【问题描述】:
我已经看到 good posts about php://input 与 $_POST,但如果我需要同时检查两者,我仍然感到困惑。或者只是检查 php://input。有什么想法吗?
我在这里做的事情是多余的吗?还是必要的?
<?php
if( $_POST ) {
// $_POST variables are set, do the stuff
} else {
// get php://input
$php_input = file_get_contents("php://input");
if( $php_input ) {
// php input exists, do the stuff
$php_postdata = json_decode($php_input);
// ...
} else {
// no $_POST and no php://input, so throw an error
echo 'Error';
exit();
}
}
?>
编辑
我不希望同时使用 $_POST 和 php://input;一个或另一个。用例是我在通过 jQuery 访问 PHP 时得到 $_POST,在通过 AngularJS 访问 PHP 时得到 php://input。
【问题讨论】:
-
这取决于您希望收到什么样的数据。
-
你想通过这个方法达到什么目的?虽然您当然可以覆盖默认使用,但在
-
php://input对PUT请求很有用 -
@dave 虽然这个问题很好,但它不是重复的。这个问题旨在专门解决检查两种类型的帖子数据的输入。