Home > Backend Development > PHP Tutorial > 借助curl理解$GLOBALS['HTTP_RAW_POST_DATA'] ,$_POST, php://input

借助curl理解$GLOBALS['HTTP_RAW_POST_DATA'] ,$_POST, php://input

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:04:19
Original
1009 people have browsed it

发送请求代码

post.php

<?php$url='http://localhost/web/curl/url.php';$data='a=123|b=2&c=3';$header=array();//$header[]="content-type:text/xml";$ch=curl_init($url);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$data);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_HTTPHEADER,$header);$con=curl_exec($ch);curl_close($ch);print_r($con);
Copy after login

接收请求的代码

url.php

<?phpecho "<h3>HTTP_RAW_POST_DATA:</h3>";print_r($GLOBALS["HTTP_RAW_POST_DATA"]);echo "<hr><h3>post:</h3>";print_r($_POST);echo "<hr> <h3>input:</h3>";print_r(file_get_contents("php://input"));echo "<hr>";echo "CONTENT_TYPE:".($_SERVER['CONTENT_TYPE']);
Copy after login

post.php执行结果

HTTP_RAW_POST_DATA:

post:

Array ( [a] => 123|b=2 [c] => 3 )

input:

a=123|b=2&c=3

CONTENT_TYPE:application/x-www-form-urlencoded

1.说明当没传content-type时 php默认是application/x-www-form-urlencoded ,此时php可识别 ,$GLOBALS["HTTP_RAW_POST_DATA"] 为空,post即为识别后生成的数组,php://input 获取原始数据

如果穿的数据时123456 $_POST里因为没key也是无数据的,php://input里还是原始数据

 

2.如果设置$header[]="content-type:text/xml";

执行结果是

HTTP_RAW_POST_DATA:

a=123|b=2&c=3

post:

Array ( )

input:

a=123|b=2&c=3

CONTENT_TYPE:text/xml

此时Post里是没数据的,HTTP_RAW_POST_DATA 里有数据

php://input 里始终是原始数据

综上所诉:

当content-type为默认的 application/x-www-form-urlencoded 时php可处理Post过来的数据并组合生成$_POST数组 $GLOBALS["HTTP_RAW_POST_DATA"] 里此时无值

当content-type为其它类型时php不能处理,生成不了$_POST数组,$GLOBALS["HTTP_RAW_POST_DATA"]里和php://input里都是原始数据

php://input在任何情况下均是最原始post来的数据

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template