php - Difference between file_get_contents and $ _POST

I'm sending data through POST from one application to another, using the file_get_contents function, I can't receive the data but using the variable $ _POST I can get it. Which would be the most suitable to receive the data? I would like to send a token for authentication together, how could I do that?

$cont = json_decode(file_get_contents('php://input'), true);
echo gettype($cont);

NULL

And when I try the pre-defined variable, I can receive the data.


$cont = $_POST;
echo gettype($cont);

array


Answer

Solution:

file_get_contents('php://input') gets the raw POST data as a string.

$_POST gets the POST data after PHP has automatically decoded it from application/x-www-form-urlencoded or multipart/form-data encodings.

If the data isn't encoded using one of those methods (e.g. if it is JSON as per your first example which uses json_decode) then $_POST won't be populated.

Source