How and why use php://input?

Recently stumbled on the trail of the php code when you create a product at REST:
$product = new Product($db);

// get posted data 
$data = json_decode(file_get_contents("php://input"));

// ensure data is not empty 
if (
 !empty($data->name) &&
 !empty($data->price) &&
 !empty($data->description) &&
) {

 // set the values of the properties of the product 
 $product->name = $data->name;
 $product->price = $data->price;
 $product->description = $data->description;
 $product->category_id = $data->category_id;
 $product->created = date('Y-m-d H:i:s');

 // creation of product 

 // set the response code - 201 created 

 // inform the user 
 echo json_encode(array("message" => "Item created."), JSON_UNESCAPED_UNICODE);

I can not understand how and what is used // get posted data

why not pass the data via POST or GET ?
April 7th 20 at 10:42
2 answers
April 7th 20 at 10:44
Data and is transmitted via POST (or GET), but there are nuances of their processing on the server side.

1. POST and GET data in the form parameter=value┬Âm2=val2 automatically handled by the server and fills the global arrays $_POST/$_GET/$_REQUEST:
$_POST['parameter'] = value;
$_POST['param2'] = val2;

GET-parameters are also automatically decoded by urldecode().
Using php://input to get the "raw" unprocessed data.

2. The POST method can be sent, for example, a JSON object, putting 'Content-type: application/json; charset=utf-8'. This array wrapper $_POST will be empty, maybe not sent ParameterName, and Parametervalue is sent only, and the server does not process such data automatically.
To obtain such data is possible only through php://input, because the global arrays $_POST/$_GET will be empty.
April 7th 20 at 10:46
this is not the answer, and copy-paste - kelsie_Schinner commented on April 7th 20 at 10:49
@kelsie_Schinnerthat after reading these links do not you understand? - London.Hansen28 commented on April 7th 20 at 10:52

Find more questions by tags PHP