Method for PHP to get HTTP body content

  • 2021-11-13 06:48:34
  • OfStack

Sometimes we need to parse the data according to the format in Header, such as uploading 1 json instead of 1 text. The concept of php input-output stream is used here.

PHP provides 1 Miscellaneous Input/Output (IO) streams that allow access to PHP's I/O streams, standard I/O and error descriptors, in-memory, temporary file streams for disk backups, and filters that can manipulate other read-write file resources.

This article covers php://input. Please refer to official website for details: Click to view official website

php://input is a read-only stream that can access the raw data of the request. In the case of POST requests, it is best to use php://input instead of $HTTP_RAW_POST_DATA because it does not depend on the specific php. ini directive. Moreover, in this case $HTTP_RAW_POST_DATA is not populated by default, potentially requiring less memory than activating always_populate_raw_post_data. php://input is invalid when enctype= "multipart/form-data".

Analog code



// server.php
switch($_SERVER['CONTENT_TYPE']){
 case 'application/json':
 //  Here, the data is obtained through the input terminal 
 $body = file_get_contents('php://input');
 echo ' This is 1 A json:', $body;
 break;
 // ...
}


// client_web.php ,   Note that the  axios
// ...
service = axios.create({
 baseURL: 'mydomain',
 validateStatus:function(status){
 if(500 >= status) alert('server fails');
 }
});

//  Hair 1 Request 
service.post(url, {
 // ...
 headers: {'Content-Type':'application/json'},
 data:'{"code":100}'
 // ...
});

//  Global settings are no problem 
service.interceptors.request.use(function(config){
 //  The request header is set here 
 config.headers['Content-Type'] = 'application/json';
 return config;
}, function(err){});

Extended reading

The following is an official document cheat sheet

PHP input and output streams are divided into: php://stdin, php://stdout and php://stderr

php://stdin, php://stdout, and php://stderr allow direct access to the corresponding input or output streams of the PHP process. The data stream references the copied file descriptor, so if you turn on php://stdin and then turn it off, you only close the copy, and the actually referenced STDIN is not affected. Note that PHP has a lot of behavior in this respect BUG up to PHP 5.2. 1. It is recommended that you simply use the constants STDIN, STDOUT, and STDERR instead of manually opening these wrappers.

Among them, php://stdin is read-only, php://stdout and php://stderr are written-only.

php://output

php://output is a write-only data stream that allows you to write to the output buffer in the same manner as print and echo 1.

php://fd

php://fd allows direct access to the specified file descriptor. For example, php://fd/3 references file descriptor 3.

php://memory and php://temp

php://memory and php://temp are a file wrapper-like data stream that allows temporary data to be read and written. The only difference between the two is that php://memory always stores data in memory, while php://temp stores it in a temporary file after the amount of memory reaches a predefined limit (the default is 2MB). Temporary file location decision and sys_get_temp_dir () mode 1.

The memory limit for php://temp can be controlled by adding/maxmemory: NN, NN is the maximum amount of data retained in memory in bytes, and if it is exceeded, temporary files are used.

php://filter

php://filter is a meta-wrapper designed for filtering applications when data streams are open. This is useful for file functions of the 1-form (all-in-one), like readfile (), file (), and file_get_contents (), with no opportunity to apply other filters before the contents of the data stream are read.

The php://filter target uses the following parameters as part 1 of its path. Composite filter chain can be specified on 1 path. You can refer to specific examples for detailed use of these parameters.


Related articles: