php中php://input的用法详细
在使用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('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.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 = 'text/html'; $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; } ?>
同样地,让我们来执行这个测试脚本,代码如下:
@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('php://input', 'r'); echo "-------$_GET------------------n"; echo var_dump($_GET) . "n"; echo "-------php://input-------------n"; echo $raw_post_data . "n"; ? <?php //@file phpinput_get.php $query_path = 'n=' . urldecode('perfgeeks') . '&p=' . urldecode('7788'); $host = '192.168.0.6'; $port = 80; $path = '/phpinput_server.php'; $d = ''; $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部分.
本文地址:
转载随意,但请附上文章地址:-)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Classification and Usage Analysis of JSP Comments JSP comments are divided into two types: single-line comments: ending with, only a single line of code can be commented. Multi-line comments: starting with /* and ending with */, you can comment multiple lines of code. Single-line comment example Multi-line comment example/**This is a multi-line comment*Can comment on multiple lines of code*/Usage of JSP comments JSP comments can be used to comment JSP code to make it easier to read

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

How to use the exit function in C language requires specific code examples. In C language, we often need to terminate the execution of the program early in the program, or exit the program under certain conditions. C language provides the exit() function to implement this function. This article will introduce the usage of exit() function and provide corresponding code examples. The exit() function is a standard library function in C language and is included in the header file. Its function is to terminate the execution of the program, and can take an integer

Introduction to Python functions: usage and examples of the abs function 1. Introduction to the usage of the abs function In Python, the abs function is a built-in function used to calculate the absolute value of a given value. It can accept a numeric argument and return the absolute value of that number. The basic syntax of the abs function is as follows: abs(x) where x is the numerical parameter to calculate the absolute value, which can be an integer or a floating point number. 2. Examples of abs function Below we will show the usage of abs function through some specific examples: Example 1: Calculation

Preparation Use vuecreateexample to create a project. The parameters are roughly as follows: use native input. Native input is mainly value and change. The data needs to be synchronized when changing. App.tsx is as follows: import{ref}from'vue';exportdefault{setup(){//username is the data constusername=ref('Zhang San');//When the input box changes, synchronize the data constonInput=;return( )=>({

Introduction to Python functions: Usage and examples of the isinstance function Python is a powerful programming language that provides many built-in functions to make programming more convenient and efficient. One of the very useful built-in functions is the isinstance() function. This article will introduce the usage and examples of the isinstance function and provide specific code examples. The isinstance() function is used to determine whether an object is an instance of a specified class or type. The syntax of this function is as follows

How to implement the laravel input hidden field: 1. Find and open the Blade template file; 2. Use the method_field method in the Blade template to create a hidden field. The creation syntax is "{{ method_field('DELETE') }}".

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or
