Home  >  Article  >  php教程  >  php中php://input的用法详细

php中php://input的用法详细

WBOY
WBOYOriginal
2016-05-25 16:43:386682browse

在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组,所以,这里主要探讨php输入流php://input.

下面的例子摘取的是wordpress中的一段代码,里面有用到http://input,有需要的可以进一步研究,代码如下:

if (!isset( $HTTP_RAW_POST_DATA ) ) {    
	    $HTTP_RAW_POST_DATA = file_get_contents('php://input');    
	} 
	// fix for mozBlog and other cases where xml isn't on the very first line    
	if ( isset($HTTP_RAW_POST_DATA) )    
	$HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);

对php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述,如下:

“php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype=”multipart/form-data”.

翻译成中文就是:

“php://input可以读取没有处理过的POST数据,相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小,并且不需要特殊的php.ini设置,php://input不能用于enctype=multipart/form-data”.

读取POST数据PHPer们一定很熟悉$_POST这个内置变量,$_POST与php://input存在哪些关联与区别呢?另外,客户端向服务端交互数据,最常用的方法除了POST之外,还有GET,既然php://input作为PHP输入流,它能读取GET数据吗?这二个问题正是我们这节需要探讨的主要内容.

经验告诉我们,从测试与观察中总结,会是一个很凑效的方法,这里,我写了几个脚本来帮助我们测试.

@file 192.168.0.6:/phpinput_server.php 打印出接收到的数据

@file 192.168.0.8:/phpinput_post.php 模拟以POST方法提交表单数据

@file 192.168.0.8:/phpinput_xmlrpc.php 模拟以POST方法发出xmlrpc请求.

@file 192.168.0.8:/phpinput_get.php 模拟以GET方法提交表单表数phpinput_server.php与phpinput_post.php

php实例代码如下:

<?php
//@file phpinput_server.php
$raw_post_data = file_get_contents(&#39;php://input&#39;, &#39;r&#39;);
echo "-------$_POST------------------n";
echo var_dump($_POST) . "n";
echo "-------php://input-------------n";
echo $raw_post_data . "n";
?>
<?php
//@file phpinput_post.php
$http_entity_body = &#39;n=&#39; . urldecode(&#39;perfgeeks&#39;) . &#39;&p=&#39; . urldecode(&#39;7788&#39;);
$http_entity_type = &#39;application/x-www-form-urlencoded&#39;;
$http_entity_length = strlen($http_entity_body);
$host = &#39;192.168.0.6&#39;;
$port = 80;
$path = &#39;/phpinput_server.php&#39;;
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp) {
    fputs($fp, "POST {$path} HTTP/1.1\r\n");
    fputs($fp, "Host: {$host}\r\n");
    fputs($fp, "Content-Type: {$http_entity_type}\r\n");
    fputs($fp, "Content-Length: {$http_entity_length}\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $http_entity_body . "\r\n\r\n");
    while (!feof($fp)) {
        $d.= fgets($fp, 4096);
    }
    fclose($fp);
    echo $d;
}
?>

我们可以通过使用工具ngrep抓取http请求包,因为我们需要探知的是php://input,所以我们这里只抓取http Request数据包,我们来执行测试脚本phpinput_post.php,代码如下:

@php /phpinput_post.phpHTTP/1.1 200 OK 
	Date: Thu, 08 Apr 2010 03:23:36 GMT 
	Server: Apache/2.2.3 (CentOS) 
	X-Powered-By: PHP/5.1.6 
	Content-Length: 160 
	Connection: close 
	Content-Type: text/html; charset=UTF-8 
	-------$_POST------------------ 
	array(2) { 
	  ["n"]=> string(9) "perfgeeks" 
	  ["p"]=> string(4) "7788" 
	} 
	-------php://input------------- 
	n=perfgeeks&p=7788通过ngrep抓到的http请求包如下: 
	 
	T 192.168.0.8:57846 -> 192.168.0.6:80 [AP] 
	  POST /phpinput_server.php HTTP/1.1.. 
	  Host: 192.168.0.6..Content-Type: application/x-www-form-urlencoded..Co 
	  ntent-Length: 18..Connection: close....n=perfgeeks&p=7788....

仔细观察,我们不难发现.

1,$_POST数据,php://input 数据与httpd entity body数据是“一致”的

2,http请求中的Content-Type是application/x-www-form-urlencoded,它表示http请求body中的数据是使用http的post方法提交的表单数据,并且进行了urlencode()处理.

我们再来看看脚本phpinput_xmlrpc.php的原文件内容,它模拟了一个POST方法提交的xml-rpc请求,代码如下:

<?php
//@file phpinput_xmlrpc.php
$http_entity_body = "nn   jt_userinfon";
$http_entity_type = &#39;text/html&#39;;
$http_entity_length = strlen($http_entity_body);
$host = &#39;192.168.0.6&#39;;
$port = 80;
$path = &#39;/phpinput_server.php&#39;;
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp) {
    fputs($fp, "POST {$path} HTTP/1.1\r\n");
    fputs($fp, "Host: {$host}\r\n");
    fputs($fp, "Content-Type: {$http_entity_type}\r\n");
    fputs($fp, "Content-Length: {$http_entity_length}\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $http_entity_body . "\r\n\r\n");
    while (!feof($fp)) {
        $d.= fgets($fp, 4096);
    }
    fclose($fp);
    echo $d;
}
?>

同样地,让我们来执行这个测试脚本,代码如下:

@php /phpinput_xmlrcp.phpHTTP/1.1 200 OK 
	Date: Thu, 08 Apr 2010 03:47:18 GMT 
	Server: Apache/2.2.3 (CentOS) 
	X-Powered-By: PHP/5.1.6 
	Content-Length: 154 
	Connection: close 
	Content-Type: text/html; charset=UTF-8 
	 
	-------$_POST------------------ 
	array(0) { 
	} 
	 
	-------php://input------------- 
	<?xml version="1.0"> 
	<methodcall> 
	   <name>jt_userinfo</name> 
	</methodcall>

执行这个脚本的时候,我们通过ngrep抓取的http请求数据包如下

T 192.168.0.8:45570 -> 192.168.0.6:80 [AP] 
	  POST /phpinput_server.php HTTP/1.1.. 
	  Host: 192.168.0.6..Content-Type: text/html..Content-Length: 75..Connec 
	  tion: close....<?xml version="1.0">.<methodcall>.   <name>jt_userinfo< 
	  /name>.</methodcall>....

同样,我样也可以很容易地发现:

1,http请求中的Content-Type是text/xml,它表示http请求中的body数据是xml数据格式.

2,服务端$_POST打印出来的是一个空数组,即与http entity body不一致了,这跟上个例子不一样了,这里的Content-Type是text/xml,而不是application/x-www-form-urlencoded

3,而php://input数据还是跟http entity body数据一致,也就是php://input数据和$_POST数据不一致了.

我们再来看看通过GET方法提交表单数据的情况,php://input能不能读取到GET方法的表单数据?在这里,我们稍加改动一下phpinput_server.php文件,将$_POST改成$_GET,代码如下:

<?php 
	//@file phpinput_server.php 
	$raw_post_data = file_get_contents(&#39;php://input&#39;, &#39;r&#39;); 
	echo "-------$_GET------------------n"; 
	echo var_dump($_GET) . "n"; 
	echo "-------php://input-------------n"; 
	echo $raw_post_data . "n"; 
	 
	? 
	<?php 
	//@file phpinput_get.php 
	$query_path = &#39;n=&#39; . urldecode(&#39;perfgeeks&#39;) . &#39;&p=&#39; . urldecode(&#39;7788&#39;); 
	$host = &#39;192.168.0.6&#39;; 
	$port = 80; 
	$path = &#39;/phpinput_server.php&#39;; 
	$d = &#39;&#39;; 
	$fp = fsockopen($host, $port, $error_no, $error_desc, 30); 
	if ($fp) { 
	  fputs($fp, "GET {$path}?{$query_path} HTTP/1.1rn"); 
	  fputs($fp, "Host: {$host}rn"); 
	  fputs($fp, "Connection: closernrn"); 
	  while (!feof($fp)) { 
	    $d .= fgets($fp, 4096); 
	  } 
	  fclose($fp); 
	  echo $d; 
	 } 
	

同样,我们执行下一phpinput_get.php测试脚本,它模拟了一个通常情况下的GET方法提交表单数据,代码如下:

@php /phpinput_get.phpHTTP/1.1 200 OK 
	Date: Thu, 08 Apr 2010 07:38:15 GMT 
	Server: Apache/2.2.3 (CentOS) 
	X-Powered-By: PHP/5.1.6 
	Content-Length: 141 
	Connection: close 
	Content-Type: text/html; charset=UTF-8 
	 
	-------$_GET------------------ 
	array(2) { 
	  ["n"]=> 
	  string(9) "perfgeeks" 
	  ["p"]=> 
	  string(4) "7788" 
	}

--php://input---在这个时候,使用ngrep工具,捕获的相应的http请求数据包如下:

T 192.168.0.8:36775 -> 192.168.0.6:80 [AP] 
	  GET /phpinput_server.php?n=perfgeeks&p=7788 HTTP/1.1.. 
	  Host: 192.168.0.6..Connection: close....

比较POST方法提交的http请求,通常GET方法提交的请求中,entity body为空,同时,不会指定Content-Type和Content-Length,但是,如果强硬数据http entity body,并指明正确地Content-Type和Content-Length,那么php://input还可是读取得到http entity body数据,但不是$_GET数据.

实践中关于php://input用法总结:

1、只有Content-Type为application/x-www-data-urlencoded时,php://input数据才跟$_POST数据相一致。

2、PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量$HTTP_RAW_POST_DATA

3、只有Coentent-Type为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会,填入的长度,由Coentent-Length指定。

4、Coentent-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST。

5、php://input数据总是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini

6、PHP会将PATH字段的query_path部分,填入全局变量$_GET,php://input读取不到$_GET数据,是因为$_GET数据作为query_path写在http请求头部(header)的PATH字段,而不是写在http请求的body部分.        
               
               

本文地址:

转载随意,但请附上文章地址:-)

Statement:
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