Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

php

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?

like image 836
Dave Moz Avatar asked Feb 23 '14 21:02

Dave Moz


People also ask

How to get JSON input in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

How to display data from JSON in PHP?

PHP File explained:Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.

How to send data in JSON format in PHP?

Send JSON data via POST with PHP cURL Initiate new cURL resource using curl_init(). Setup data in PHP array and encode into a JSON string using json_encode(). Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option. Set the Content-Type of request to application/json using the CURLOPT_HTTPHEADER option.

What is file_ get_ contents('PHP input')?

The command file_get_contents('php://input') reads the raw information sent to PHP -- unprocessed before it ever gets put into $_POST or $_REQUEST super globals. This technique is often used when someone is uploading a file, such as an image.


2 Answers

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

like image 77
AKASH VERMA Avatar answered Oct 21 '22 04:10

AKASH VERMA


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.

like image 41
elixenide Avatar answered Oct 21 '22 04:10

elixenide