PHP: file_get_contents('php://input') returning string for JSON message

前端 未结 7 1468
梦毁少年i
梦毁少年i 2020-12-19 04:26

I am trying to read in a JSON message in my PHP app and this is my php code:

$json = file_get_contents(\'php://input\');
$obj = json_decode($json, TRUE);
ech         


        
7条回答
  •  粉色の甜心
    2020-12-19 04:38

    Your use of json_decode is creating an associative array, not an object. You can treat it like an array, instead of an object. If you want an object, use this, instead:

    $obj = json_decode($json);
    

    See the documentation on the second parameter to json_decode():

    assoc When TRUE, returned objects will be converted into associative arrays.

    Also, as Johannes H. pointed out in the comments, the output of echo $json; indicates that you are not actually receiving JSON, in the first place, so you will need to address that, as well. You asked why it isn't JSON; without seeing how you are requesting this script, it's impossible to say for sure.

提交回复
热议问题