File_Get_Contents("PHP://Input") or $Http_Raw_Post_Data, Which One Is Better to Get the Body of Json Request

file_get_contents(php://input) or $HTTP_RAW_POST_DATA, which one is better to get the body of JSON request?

Actually php://input allows you to read raw request body.

It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.
From Reference

php://input is not available with enctype="multipart/form-data".

Not getting post value from file_get_contents('php://input')

  1. Check how is your request created. It must be POST and php://input is not available for enctype="multipart/form-data"

  2. Problem may be json_encode() receiving invalid JSON. Check for json_decode error and/or check if the output from file_get_contents('php://input') is really empty.

get/read raw post data from php://input using CURL and not using file_get_contents

to my another question related to this i got an alternate. and it worked.

what are the alternatives for php://input and $HTTP_RAW_POST_DATA when file_get_contents and always_populate_raw_post_data are disabled

since file_get_contents was disabled and curl was the only way for communication i was trying to use wrappers in curl like php;//input which i think no possible yet i got this alternate.

so the link is an alternate for file_get_contents("php://input");

Accessing and decoding JSON sent from JavaScript to PHP

You can't get your data from $_POST if you put JSON in your post body.
see this question Receive JSON POST with PHP. php can't handle application/json properly.

For your var_dump is empty, try this

var_dump(file_get_contents('php://input'));
var_dump(json_decode(file_get_contents('php://input'), true));

you will see your data.

And if you send your data without change it to JSON, you will get wrong data.

eg: your finalArray is ['a','b','c'] and you send it directly.

var_dump(file_get_contents('php://input'));

you will see php got string a,b,c instead of ['a','b','c']

So if you want to use $_POST to receive data, you need to use application/x-www-form-urlencoded. you can use jquery to do it. see http://api.jquery.com/jquery.ajax/

 $.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});

it will serialize your js object into x-www-form-urlencoded and php will handle it properly.

use chrome's dev tools, switch to network and see the request payload and response would be helpful for you.



Related Topics



Leave a reply



Submit