首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP输入流php://input介绍

PHP输入流php://input介绍

作者头像
wangxl
发布2018-03-07 15:20:02
2.1K0
发布2018-03-07 15:20:02
举报
文章被收录于专栏:PHP在线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 <?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"; ?> <?php //@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.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.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()处理。 (注:注意加粗部分内容,下文不再提示).

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-04-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 php 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档