$GLOBALS['HTTP_RAW_POST_DATA']、$_POST和php://input深刻探究三者的区别


$_POST:经过 HTTP POST 方法传递的变量组成的数组。是自动全局变量。php

$GLOBALS['HTTP_RAW_POST_DATA'] :老是产生 $HTTP_RAW_POST_DATA 变量包含有原始的 POST 数据。此变量仅在碰到未识别 MIME 类型的数据时产生。数组

$HTTP_RAW_POST_DATA 对于 enctype="multipart/form-data" 表单数据不可用。app

也就是说基本上$GLOBALS['HTTP_RAW_POST_DATA'] 和 $_POST是同样的。post

可是若是post过来的数据不是PHP可以识别的,你能够用 $GLOBALS['HTTP_RAW_POST_DATA']来接收,好比 text/xml 或者 soap 等等。url

补充说明:PHP默认识别的数据类型是application/x-www.form-urlencoded标准的数据类型。code


咱们来看看手册是怎么说?orm

老是产生变量包含有原始的 POST 数据。不然,此变量仅在碰到未识别 MIME 类型的数据时产生。不过,访问原始 POST 数据的更好方法是 php://input。server

$HTTP_RAW_POST_DATA 对于 enctype="multipart/form-data" 表单数据不可用。xml


可是:$HTTP_RAW_POST_DATA == $_POST 吗?ip

答案确定不同。手册这么说:

The RAW / uninterpreted HTTP POst information can be accessed with:
$GLOBALS['HTTP_RAW_POST_DATA']
This is useful in cases where the post Content-Type is not something PHP understands (such as text/xml).

也就是说,基本上$GLOBALS['HTTP_RAW_POST_DATA'] 和 $_POST是同样的。可是若是post过来的数据不是PHP可以识别的,你能够用 $GLOBALS['HTTP_RAW_POST_DATA']来接收,好比 text/xml 或者 soap 等等。


用Content-Type=text/xml 类型,提交一个xml文档内容给了php server,要怎么得到这个POST数据。 The RAW / uninterpreted HTTP POST information can be accessed with: $GLOBALS['HTTP_RAW_POST_DATA'] This is useful in cases where the post Content-Type is not something PHP understands (such as text/xml). 因为PHP默认只识别application/x-www.form-urlencoded标准的数据类型,所以,对型如text/xml的内容没法解析为$_POST数组,故保留原型,交给$GLOBALS['HTTP_RAW_POST_DATA'] 来接收。 另外还有一项 php://input 也能够实现此这个功能 php://input 容许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,而且不须要任何特殊的 php.ini 设置。php://input 不能用于 enctype="multipart/form-data"。 上个例子吧: ------------------------------------------------------------- <form action="post.php" method="post"> <input type="text" name="user"> <input type="password" name="password"> <input type="submit"> </form> ------------------------------------------------------------- <?php echo file_get_contents("php://input"); ?>

相关文章
相关标签/搜索