1

I've already seen good posts about php://input vs $_POST, but I'm still confused if I need to check for both. Or just check for 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; 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.

Community
  • 1
  • 1
johnkeese
  • 4,048
  • 3
  • 18
  • 15
  • 1
    That depends on what kind of data you’re expecting to receive. – CBroe Aug 08 '14 at 20:54
  • What do you want to achieve by using this method? While you certainly could override the default use, in < 99% it's not needed nor even useful – Francisco Presencia Aug 08 '14 at 20:55
  • `php://input` is useful for `PUT` requests – Xeoncross Aug 08 '14 at 20:55
  • 2
    possible duplicate of [PHP "php://input" vs $\_POST](http://stackoverflow.com/questions/8893574/php-php-input-vs-post) – dave Aug 08 '14 at 21:01
  • @dave while that question is good, it is not a duplicate. This question is meant to specifically address checking for input from both types of post data. – johnkeese Aug 11 '14 at 14:45
  • @FranciscoPresencia I want the PHP file to be able to accept **both** $_POST **and** php://input. And once accept it, know how to handle it appropriately. – johnkeese Aug 11 '14 at 14:47

1 Answers1

0

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:

$_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:

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.

Community
  • 1
  • 1
dave
  • 62,300
  • 5
  • 72
  • 93
  • Sorry for the confusion. Yes, I am expecting JSON in the php://input -- specifically from AngularJS $http. But I am not expecting $_POST and php://input in the same request. I'll edit the question to be more specific. – johnkeese Aug 11 '14 at 21:59