PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php获取输入流

原创
2016-06-06 19:49:25 1287浏览

uc中的用到的代码(在api/uc.php)代码: $post = xml_unserialize(file_get_contents('php://input'));? php手册(http://cn.php.net/manual/zh/wrappers.php.php)说明: php://input allows you to read raw data from the request body. In case of POST re

uc中的用到的代码(在api/uc.php)代码:

$post = xml_unserialize(file_get_contents('php://input'));?

php手册(http://cn.php.net/manual/zh/wrappers.php.php)说明:

php://input allows you to read raw data from the request body. In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

Note: A stream opened with php://input can only be read once; the stream does not support seek operations. However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.

理解

1.符号的理解:与符号”http://“对比。可以理解php://表示这是一种协议。不同的是它是php自己定义与使用的协议。

输入域名的时候,htt://后面输入的是域的名字。那么php://后面的"input"也可以表示一个文件。是一个php已经定义好的文件。比如有ouput。php自己知道去哪里找这个文件。

2.相关联知识: 常量$HTTP_RAW_POST_DATA是获取post提交的原始数据(contains the raw POST data)。php://input 的所完成的功能与它一样:都是包含post过来的原始数据。

使用php://input比$HTTP_RAW_POST_DATA更好。因为:使用php://inpu不受到配置文件的影响。常量$HTTP_RAW_POST_DATA受到php.ini配置的影响。在php.ini中有个选项,always_populate_raw_post_data = On。该配置决定是否生成常量$HTTP_RAW_POST_DATA(保存post数据)。

php://input的方式与$HTTP_RAW_POST_DATA数据都同样不适用于表单提交的"multipart/form-data"类型的数据。所以,两种的区别就是在于是否受到配置文件的影响。另外,内存消耗更少,这个怎么理解?

3.怎么得到其内容:使用file_get_contents('php://input')得到输入流的内容。php://input的结果只读。

 

想更好地理解它是怎么使用的。做个试验就明白:

html文件输入内容:





无标题文档















input.php:


$input = file_get_contents('php://input');
var_dump($input);exit;
?>

提交后,得到结果:

string(97) "username=%E5%90%8D%E5%AD%97&password=%E5%AF%86%E7%A0%81&name=%E6%8F%90%E4%BA%A4%E6%95%B0%E6%8D%AE"

接下来改为get方式提交,结果为:

string(0) ""

说明没有任何数据


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。