11

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);
echo $obj->{'S3URL'};

When I do this I am getting the following error:

Trying to get property of non-object in setImage.php on line 25 (line 25 is the echo $obj->{'S3URL'}; line)

This is the request body of the request to the page:

Request Url: http://localhost:8888/setImage.php
Request Method: POST
Status Code: 200
Params: {
   "S3URL": "http://url.com"
}

This is the request headers:

Accept: application/json
Content-Type: application/json
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML,

However, if I instead echo out the $json variable I get the following:

S3URL=http%3A%2F%2Furl.com

So it looks like file_get_contents('php://input'); is reading it in as a string, and not as JSON, which will make parsing it more difficult.

Any idea why it isn't being returned as JSON, or how I can get it to be returned as JSON?

Seonghyeon Cho
  • 171
  • 1
  • 3
  • 11
Dave Moz
  • 127
  • 1
  • 1
  • 6
  • 4
    json_decode($json, TRUE); returns array not object. – Abhik Chakraborty Feb 23 '14 at 21:41
  • 1
    'php://input' always fetches the request body as-is. Therefore, if you don'T get JSON code, no JSON code was sent. Period. As there is an `=` in the string what you get, the form was most likely sent as `application/x-www-form-urlencoded` – Johannes H. Feb 23 '14 at 21:43
  • Abhik, yeah, I tried removing the TRUE, but still no dice. Thanks Johannes H, I am using the REST Console for sending the data, and from the content headers of the request it looks like it should be sending it as JSON, but maybe it isn't? – Dave Moz Feb 23 '14 at 21:50

7 Answers7

8

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.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 4
    While correct, this isn't the only issue in the code. If `echo $json;` outputs `S3URL=http%3A%2F%2Furl.com` as stated in the question, no JSON was sent in the first place. – Johannes H. Feb 23 '14 at 21:46
  • Right, even removing the "TRUE" hasn't solved the problem. I think Johannes is correct here. – Dave Moz Feb 23 '14 at 21:52
7

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'];

AKASH VERMA
  • 99
  • 1
  • 1
0

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

Use this one result will

$chd = json_decode(file_get_contents('php://input'), TRUE);
$childs = implode("",$chd);
David
  • 3,285
  • 1
  • 37
  • 54
0

Try this https://stackoverflow.com/a/56801419/7378998

It works for JSON or jquery post

$.post(url, $('form').serialize());
0

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.

Ansh Mehta
  • 28
  • 6
-1

$obj = json_decode($json);

Just remove the true

ramin
  • 17
  • 3