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 withenctype="multipart/form-data"
.
Not getting post value from file_get_contents('php://input')
Check how is your request created. It must be POST and php://input is not available for enctype="multipart/form-data"
Problem may be
json_encode()
receiving invalid JSON. Check forjson_decode
error and/or check if the output fromfile_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
Selecting Unique Values from a Column
How to Truncate a String to the First 20 Words in PHP
How to Load a Controller from Another Controller in Codeigniter
Warning About '$Http_Raw_Post_Data' Being Deprecated
PHP Pdoexception: "Sqlstate[Hy093]: Invalid Parameter Number"
Where to Put Password_Verify in Login Script
Best Way to Parse Rss/Atom Feeds With PHP
Guzzlehttp - How Get the Body of a Response from Guzzle 6
PHPmailer - Smtp Error: Password Command Failed When Send Mail from My Server
Curl Error 60, Ssl Certificate Issue: Self Signed Certificate in Certificate Chain
Weird PHP Error: 'Can't Use Function Return Value in Write Context'
Pagination Using MySQL Limit, Offset
Fastest Way to Check If a String Is Json in PHP