您的位置:首页 > 理论基础 > 计算机网络

$POST 、$GLOBALS['HTTP_RAW_POST_DATA']、php://input三者之间的区别

2017-02-23 18:20 609 查看
三者都是产生包含有原始的 POST 数据的变量。

不过,访问原始 POST 数据的更好方法是 php://input。

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

1:$POST我们常用的就不用介绍了吧

2:$GLOBALS['HTTP_RAW_POST_DATA']

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

PHP默认识别的数据类型是Content-Type=application/x-www.form-urlencoded标准的数据类型,如果是Content-Type=text/xml 类型,无法解析为$_POST数组,故保留原型提交一个xml文档内容给了php
server,要使用$GLOBALS['HTTP_RAW_POST_DATA'] 获得这个POST数据。
PHP7移除了$GLOBALS["HTTP_RAW_POST_DATA"]这个全局变量,建议用php://input方法代替

3:php://input

php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。

例子:

1.php

<?php
echo "<pre>";
print_r($_POST);
$data = file_get_contents('php://input');   //都要解下码
var_dump(urldecode($data));
echo "<br>";
var_dump(urldecode($GLOBALS['HTTP_RAW_POST_DATA']));
?>

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#b01").click(function(){
$.ajax({
//提交数据的类型 POST GET
type:"POST",
//提交的网址
url:"1.php",
//提交的数据
data:{Name:"sanmao",Password:"sanmaoword"},
//返回数据的格式
datatype: "html",//"xml", "html", "script", "json", "jsonp", "text".
//编码类型
contentType:"application/x-www-form-urlencoded",
//成功返回之后调用的函数
success:function(data){
//$("#msg").html(decodeURI(data));
},
//调用执行后调用的函数
complete: function(XMLHttpRequest, textStatus){
//alert(XMLHttpRequest.responseText);
//alert(textStatus);
//HideLoading();
},
//调用出错执行的函数
error: function(){
//请求出错处理
}
});
});
$("#b02").click(function(){
$.ajax({
//提交数据的类型 POST GET
type:"POST",
//提交的网址
url:"1.php",
//提交的数据
data:{Name:"sanmao",Password:"sanmaoword"},
//返回数据的格式
datatype: "html",//"xml", "html", "script", "json", "jsonp", "text".
//编码类型
contentType:"application/json",
//成功返回之后调用的函数
success:function(data){
//$("#msg").html(decodeURI(data));
},
//调用执行后调用的函数
complete: function(XMLHttpRequest, textStatus){
//alert(XMLHttpRequest.responseText);
//alert(textStatus);
//HideLoading();
},
//调用出错执行的函数
error: function(){
//请求出错处理
}
});
});
});
</script>
</head>
<body>
<button id="b01" type="button">application/x-www-form-urlencoded</button>
<button id="b02" type="button">application/json</button>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据 php POST