Read input stream in PHP

When we are reading a request about a file PHP the most normal thing is to read the $_GET and $_POST variables. But it may be the case that we want to read the input stream at PHP directly. That is, read the information that comes independently of the variables.

Read the input stream at PHP can be very useful in cases in which we send an XML or a JSON object to the server.

To be able to read the input stream at PHP we are going to rely on the “php://” element. At PHP Using the «php://» element we can access different streams from our programs, whether they are input streams or output streams, file streams, error streams,…

In the case of the input stream we are going to use the php://input statement. As it is an input stream, we are going to rely on the file_get_contents function.

$data = file_get_contents('php://input');

In this way we already have saved in the variable $datos all the input that has been sent to our file PHP.

We can now download it through the console, manipulate it,… do whatever we need with it. For example, dump it on the console.

var_dump($data);

I hope that with this article you are now able to read the input stream at PHP.

Leave a Comment