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

php file_put_contents
php file_get_contents post
php file_get_contents not working
php file_get_contents json
php file_get_contents https
php file_get_contents bool false
php read file line by line
php read file into array

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?

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.

PHP file_get_contents() Function, The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are​  Definition and Usage The file_get_contents () reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

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

PHP, In PHP, file_get_contents() function is one of the file handling functions available in this scripting language. This function reads entire file  PHP - Function file_get_contents() - This function is identical to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. Home Jobs

$obj = json_decode($json);

Just remove the true

PHP file_get_contents(), php $filestring = file_get_contents($filename); print $filestring; ?> $Filename, as mentioned already, is a variable used to represent a file you have chosen already,  The file_get_contents () function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file. The path of the file to be read is sent as a parameter to the function and it returns the data read on success and FALSE on failure.

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.

file_get_contents() and file() – Hacking with PHP, PHP - Function file_get_contents() - This function is identical to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to  PHP inbuilt file_get_contents () function is used to read a file into a string. This can read any file or API data using URLs and store data in a variable. This function is the easiest method to read any data by passing API URL. In the following PHP program, we will use a sample API URL that provides employee’s data and displays them.

Use this one result will

$chd = json_decode(file_get_contents('php://input'), TRUE);
$childs = implode("",$chd);

PHP - Function file_get_contents(), The file_get_contents() function reads entire file into a string. The file() function reads the entire file in a array, whereas file_get_contents()  Basically, you have to create a stream, with the right options (there is a full list on that page), and use it as the third parameter to file_get_contents -- nothing more ;-)

file_get_contents() function in PHP, file_get_contents. (PHP 4 >= 4.3.0, PHP 5, PHP 7). file_get_contents — Reads entire file into a string  Browse other questions tagged php file return file-get-contents or ask your own question. The Overflow Blog The Overflow #26: The next right thing

file_get_contents(), file_get_contents. (PHP 4 >= 4.3.0, PHP 5, PHP 7). file_get_contents — Liest die gesamte Datei in einen String  As of PHP 5.6 the file(), file_get_contents(), and fopen() functions will return false if you are referencing a source URL that doesn't have a valid SSL certificate. Presumably, you will run into this a lot in your development environments this will drive you crazy.

PHP: file_get_contents, Your setup may have allow_url_fopen not set up. You need to set up a configuration directive in order to be able to open files via HTTP, because by default  However the file_get_contents does not get the contents compressed. It requests the server to send everything in plain text format. Most websites are capable of serving compressed content, if they are asked to do so in the http headers.

Comments
  • json_decode($json, TRUE); returns array not object.
  • '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
  • 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?
  • 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.
  • Right, even removing the "TRUE" hasn't solved the problem. I think Johannes is correct here.