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

前端 未结 7 1463
梦毁少年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:29

    If you are using javascript to send JSON, to a php file

    send_recieve.js

    var myObject = JSON.stringify({"name":"John","age":30,"city":"New York"});
    var xhr = new XMLHttpRequest();
    xhr.open("POST","http://localhost/dashboard/iwplab/j-comp/receive_send.php",false);
    xhr.setRequestHeader("Content-type","application/json");
    xhr.onreadystatechange = function(){
        if(xhr.readyState==4){console.log("xhr response:"+xhr.response)}
        alert(xhr.responseText);
     };
     xhr.send(myObject);
    

    recieve_send.php

    <?php
        $var = json_decode(file_get_contents("php://input"),true);
        echo "Data recieved by PHP file.\n";
        if ($var["name"]=="John"){
            echo "a";
        }
        else{
            echo "b";
        }
        //Manipulate/validate/store/retrieve to database here
        //echo statements work as response
        echo "\nSent";
    
    ?>
    

    echo statements work as a response.

    0 讨论(0)
  • 2020-12-19 04:33

    The problem may be form php://input (is a read-only stream that allows you to read raw data from the request body). change some setting from php.ini , try to make "allow_url_fopen" on.

    0 讨论(0)
  • 2020-12-19 04:36

    there are two type for executing this type of request

    First : you can use it as an stdClassObject for this

    $data = json_decode(file_get_contents('php://input'));

    it will return a object and you can retrieve data from this like

    $name = $data->name;

    Second : you can use it as an array for this

    $data = json_decode(file_get_contents('php://input'), true);

    it will return a object and you can retrieve data from this like

    $name = $data['name'];

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 04:49

    Use this one result will

    $chd = json_decode(file_get_contents('php://input'), TRUE);
    $childs = implode("",$chd);
    
    0 讨论(0)
  • 2020-12-19 04:50

    $obj = json_decode($json);

    Just remove the true

    0 讨论(0)
提交回复
热议问题