json - Echo the STDIN given to a PHP script -
I'm trying to use PHP for a command line script. I pass a JSON string to it, and I'm trying to read the values, but when I my_test.php The Run it from the command line to pass JSON: I'm getting this error: default as if string json_decode converts the JSON PHP object you can use the second parameter if you want to get PHP array, json_decode: echo $ user_inputs [ "foo"] , so why is this? Am I forgetting about json_decode, or is it about using STDIN?
// Get Steediaian to $ Stdin = fopen ( 'php: // stdin', 'r'); // start user_inputs_json which will be full studine $ user_inputs_json = ""; // read all stdin while ($ line = fgets ($ stdin)) {$ user_inputs_json = $ line; } // Create the decoded Jason object. $ User_inputs = json_decode ($ user_inputs_json); // Try to resize a value This is where I get my error (written below). Echo $ user_inputs ["foo"]; Fclose ($ stdin);
$ echo '{ "foo": " Hello world! "," Bar ":" Goodnight moon! "} '| Php my_test.php
can not use the object Fatal error: Type stdClass in / users / Don / desktop / my_test.php row
$ user_inputs_array = json_decode ($ user_inputs_json, true) ;
Comments
Post a Comment