php获取输入流

news/2024/7/7 18:42:36

uc中的用到的代码(在api/uc.php)代码:

$post = xml_unserialize(file_get_contents('php://input'));

php手册(http://cn.php.net/manual/zh/wrappers.php.php)说明:

php://input allows you to read raw data from the request body. In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

Note: A stream opened with php://input can only be read once; the stream does not support seek operations. However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.

理解

1.符号的理解:与符号”http://“对比。可以理解php://表示这是一种协议。不同的是它是php自己定义与使用的协议。

输入域名的时候,htt://后面输入的是域的名字。那么php://后面的"input"也可以表示一个文件。是一个php已经定义好的文件。比如有ouput。php自己知道去哪里找这个文件。

2.相关联知识: 常量$HTTP_RAW_POST_DATA是获取post提交的原始数据(contains the raw POST data)。php://input 的所完成的功能与它一样:都是包含post过来的原始数据。

使用php://input比$HTTP_RAW_POST_DATA更好。因为:使用php://inpu不受到配置文件的影响。常量$HTTP_RAW_POST_DATA受到php.ini配置的影响。在php.ini中有个选项,always_populate_raw_post_data = On。该配置决定是否生成常量$HTTP_RAW_POST_DATA(保存post数据)。

php://input的方式与$HTTP_RAW_POST_DATA数据都同样不适用于表单提交的"multipart/form-data"类型的数据。所以,两种的区别就是在于是否受到配置文件的影响。另外,内存消耗更少,这个怎么理解?

3.怎么得到其内容:使用file_get_contents('php://input')得到输入流的内容。php://input的结果只读。

 

4.使用规则:必须是post方式提交的数据。而且提交的不能是multipart/form-data类型的数据(当需要上传文件,就会在form标签中添加属性enctype="multipart/form-data")

想更好地理解它是怎么使用的。做个试验就明白:

html文件输入内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

<form action="input.php" method="post">


<input name="username" type="text" />


<input name="password" type="text" />
<input name="name" type="submit"value="提交数据" />
</form>

</body>
</html>

input.php:

<?php

$input = file_get_contents('php://input');
var_dump($input);exit;
?>

提交后,得到结果:

string(97) "username=%E5%90%8D%E5%AD%97&password=%E5%AF%86%E7%A0%81&name=%E6%8F%90%E4%BA%A4%E6%95%B0%E6%8D%AE"

接下来改为get方式提交,结果为:

string(0) ""

说明没有任何数据



http://lihuaxi.xjx100.cn/news/254276.html

相关文章

Pandas 实用技能,将列(column)排序的几种方法

作者 | 阳哥来源 | Python数据之道Pandas 可以说是 在Python数据科学领域应用最为广泛的工具之一。Pandas是一种高效的数据处理库&#xff0c;它以 dataframe 和 series 为基本数据类型&#xff0c;呈现出类似excel的二维数据。在数据处理过程中&#xff0c;咱们经常需要将列按…

Numpy入门教程:06. 排序,搜索和计数

背景 什么是 NumPy 呢&#xff1f; NumPy 这个词来源于两个单词 – Numerical和Python。其是一个功能强大的 Python 库&#xff0c;可以帮助程序员轻松地进行数值计算&#xff0c;通常应用于以下场景&#xff1a; 执行各种数学任务&#xff0c;如&#xff1a;数值积分、微分、…

设置列表字段为主键

转贴:Sample event handler to set a field as a pr imary key (enforce no duplicates) Got this as a request from a reader- how to prevent users from adding items with same titles as ones that already exist in the list. Codeusing System;using System.Collectio…

Largest Rectangle in a Histogram

ps&#xff1a;单调栈&#xff0c;注意红色部分的代码。 int n;stack<P> s;inline void upd(LL &x, LL y) { (x < y) && (x y); }int main() {while(sc(n) ! EOF && n) {while(!s.empty()) s.pop();LL ans 0;Rep(i, 1, n) {int x;sc(x);if (s.e…

AI大神LeCun深度学习公开课来啦!4万字干货笔记

Datawhale干货 主讲&#xff1a;Yann LeCun&#xff0c;整理&#xff1a;新智元【新智元导读】Yann LeCun大师课程&#xff0c;搭配120页笔记食用效果更佳。喜欢深度学习&#xff1f;最好的方法就是在线课程。这里推荐图灵奖得主、纽约大学教授Yann LeCun主讲的在线课程。该课程…

云淘金时代,安全为王

在数字经济和技术生态高质量发展的今天&#xff0c;企业对前沿技术和高质量人才的需求不断升级。为了帮助更多开发者、企业洞察行业趋势、技术热点&#xff0c;CSDN 重磅打造技术访谈金牌栏目《架构师说》&#xff0c;聚焦数字化转型、云原生、数据库、开源技术、人工智能、出海…

Nginx 面试 40 连问,快顶不住了~~

欢迎关注方志朋的博客&#xff0c;回复”666“获面试宝典什么是Nginx&#xff1f;Nginx是一个 轻量级/高性能的反向代理Web服务器&#xff0c;用于 HTTP、HTTPS、SMTP、POP3 和 IMAP 协议。他实现非常高效的反向代理、负载平衡&#xff0c;他可以处理2-3万并发连接数&#xff0…

学好计算机第一步

Datawhale干货 作者&#xff1a;cxuan审校&#xff1a;黄元帅前 言计算机已经发展了半个世纪之久&#xff0c;我们从一出生就能享用计算机高速发展的成果&#xff0c;但我们从未对计算机产生过敬畏之心&#xff0c;为什么&#xff1f;因为我们不了解计算机&#xff0c;何谈敬畏…