What is php://input? How to use php://input?

14 April 2023 479 Reading time: 41 second

php://input is a special stream type in PHP that provides access to the body of HTTP requests received by your PHP script. Particularly in POST, PUT, and DELETE requests, request data is carried in the HTTP body. The php://input stream allows you to read this body data directly.

For instance, if you want to process a JSON body, you can use the php://input stream as follows:

$json = file_get_contents('php://input');
$data = json_decode($json, true); // the true parameter ensures that the result is an array

This code loads the JSON data in the HTTP request body into a variable, and then converts this data into an array that you can use in your PHP code.

The php://input stream is commonly used in some PHP frameworks and custom API processing scenarios.

Similar articles