php (//input),PHP 输入流 php://input使用说明(1/2)_PHP教程

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

对一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数据

不能用于multipart/form-data类型

php://input VS $HTTP_RAW_POST_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

代码如下

复制代码

//@file phpinput_server.php

$raw_post_data = file_get_contents(‘php://input’, ‘r’);

echo “——-$_POST——————n”;

echo var_dump($_POST) . “n”;

echo “——-php://input————-n”;

echo $raw_post_data . “n”;

?>

//@file phpinput_post.php

$http_entity_body = ‘n=’ . urldecode(‘perfgeeks’) . ‘&p=’ . urldecode(‘7788’);

$http_entity_type = ‘application/x-www-form-urlencoded’;

$http_entity_length = strlen($http_entity_body);

$host = ‘192.168.0.6’;

$port = 80;

$path = ‘/phpinput_server.php’;

$fp = fsockopen($host, $port, $error_no, $error_desc, 30);

if ($fp) {

fputs($fp, “POST {$path} HTTP/1.1rn”);

fputs($fp, “Host: {$host}rn”);

fputs($fp, “Content-Type: {$http_entity_type}rn”);

fputs($fp, “Content-Length: {$http_entity_length}rn”);

fputs($fp, “Connection: closernrn”);

fputs($fp, $http_entity_body . “rnrn”);

while (!feof($fp)) {

$d .= fgets($fp, 4096);

}

fclose($fp);

echo $d;

}

?>

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

代码如下

复制代码

@php /phpinput_post.php

HTTP/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()处理。

(注:注意加粗部分内容,下文不再提示).

1 2

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