输入流 php://input
- 1.仅在取值为
application/x-www-data-urlencoded
和multipart/form-data
时(文件上传时),php会将http请求body相应数据会填入到数组$_POST,填入到$_POST数组中的数据是进行urldecode()解析的结果。 - 2.只要Content-Type不为
multipart/form-data
,php://input
会填入post数据。 - 3.仅当Content-Type为
application/x-www-form-urlencoded
且提交方法是POST方法时,$_POST数据与php://input
数据才是一致的。
php://input
可以读取没有处理过的POST数据。相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小。解析var_dump($_POST);echo file_get_contents("php://input");情况1: 文件上传时,form的enctype="multipart/form-data",
此时,数据php://input
获取不到数据<form enctype="multipart/form-data" method="post"><input type="text" name="name" /><input type="file" name="csv_file" /><button type="submit" name="submit" value="Submit">Submit</button></form><?phpvar_dump($_POST);echo "<br>";var_dump(file_get_contents("php://input"));测试结果 只有$_POST
获取了数据array (size=2)'name' => string 'test' (length=4)'submit' => string 'Submit' (length=6)情况2: 非文件上传时的form表单<form method="post">name:<input type="text" name="name" /><br>age:<input type="text" name="age" /><button type="submit" name="submit" value="Submit">Submit</button></form><?phpecho '$_POST result:<br>';var_dump($_POST);echo "<br>";echo 'php://input result:<br>';var_dump(file_get_contents("php://input"));测试结果:两者都会获取数据$_POST result:array (size=3)'name' => string 'revin' (length=5)'age' => string '28' (length=2)'submit' => string 'Submit' (length=6)php://input result:string 'name=revin&age=28&submit=Submit' (length=31)情况3 :postman 直接发送json body 体, 也就是api的场景测试结果:只有php://input
获取到了数据
Last modified 3yr ago