简体   繁体   English

检查“ php:// input”和$ _POST

[英]Checking for “php://input” AND $_POST

I've already seen good posts about php://input vs $_POST, but I'm still confused if I need to check for both. 我已经看过有关 php:// input vs $ _POST的 帖子 ,但是如果我需要检查两者,我仍然感到困惑。 Or just check for php://input. 或者只是检查php:// input。 Any thoughts? 有什么想法吗?

Is what I'm doing here redundant? 我在这里做的事多余吗? Or necessary? 还是有必要?

<?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();
        }
    }
?>

EDIT 编辑

I'm not expecting both $_POST and php://input at the same time; 我不希望同时$ _POST和php:// input; either one or the other. 一个或另一个。 Use case is I get $_POST when hitting the PHP via jQuery and I get php://input when hitting the PHP via AngularJS. 用例是当我通过jQuery击中PHP时得到$ _POST,而当我通过AngularJS击中PHP时得到php:// input。

Your question was "do I need to check for both, or just one." 您的问题是“我需要同时检查两个还是仅检查一个”。 This question's answer explains exactly what will be in each, and the differences between them. 这个问题的答案准确地解释了每一个的含义以及它们之间的差异。

There is no way for us to know which one you need to check, but if you read that question and it's answer, YOU will know which one has the data you need. 我们无法知道您需要检查哪一个,但是如果您阅读了该问题及其答案,您将知道哪一个具有所需的数据。 If you just need to merge both inputs, we would need to know what kind of data you expect to be getting - if it is coming in as application/json , for example, you could do: 如果您只需要合并两个输入,则我们需要知道您期望获得哪种数据-例如,如果它以application/json ,则可以执行以下操作:

$_POST = array_merge($_POST, json_decode(file_get_contents("php://input"), true));

EDIT: knowing that this is for angular, I would either overwrite the $_POST field with the json_decoded input: 编辑:知道这是针对角度的,我要么用json_decoded输入覆盖$ _POST字段:

if (empty($_POST)) {
    $_POST = json_decode(file_get_contents("php://input"), true));
}

Or make Angular POST the data in a way that will show up in the $_POST variable, there is a very detailed guide here which explains how to do that. 或者将Angular POST的数据显示在$_POST变量中,此处有一个非常详细的指南,其中介绍了如何执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM