PHP" PHP://输入" VS $ _ POST [英] PHP "php://input" vs $_POST
问题描述
我已指示使用的方法 PHP://输入
,而不是 $的_ POST
使用Ajax交互时从JQuery的请求。我不明白的是使用这种VS $ _ POST
或 $ _ GET
的全局方法的好处。
I have been directed to use the method php://input
instead of $_POST
when interacting with Ajax requests from JQuery. What I do not understand is the benefits of using this vs the global method of $_POST
or $_GET
.
推荐答案
原因是, PHP://输入
返回所有的HTTP报头后的原始数据的请求,而不论该内容类型的
The reason is that php://input
returns all the raw data after the HTTP-headers of the request, regardless of the content type.
$ _ POST PHP的超全局
The PHP superglobal $_POST
, only is supposed to wrap data that is either
-
应用程序/ x-WWW的形式urlen codeD
(适用于简单的形式,员额的标准内容类型)或 -
的multipart / form-data的
克斯codeD(主要用于文件上传)
application/x-www-form-urlencoded
(standard content type for simple form-posts) ormultipart/form-data
-encoded (mostly used for file uploads)
这是因为只有这些内容类型的必须的是由用户代理支持的。因此,服务器和PHP历来不希望收到任何其他内容类型(这并不意味着他们不能)。
This is because these are the only content types that must be supported by user agents. So the server and PHP traditionally don't expect to receive any other content type (which doesn't mean they couldn't).
所以,如果你只是岗位创先争优旧的HTML 表格
,请求看起来是这样的:
So, if you simply POST a good old HTML form
, the request looks something like this:
POST /page.php HTTP/1.1
key1=value1&key2=value2&key3=value3
但是,如果你正在使用的Ajax了很多,这probaby还包括与类型(字符串,整型,布尔)和结构(数组,对象)交换更复杂的数据,所以在大多数情况下,JSON是最好的选择。但随着JSON有效载荷的要求会是这个样子:
But if you are working with Ajax a lot, this probaby also includes exchanging more complex data with types (string, int, bool) and structures (arrays, objects), so in most cases JSON is the best choice. But a request with a JSON-payload would look something like this:
POST /page.php HTTP/1.1
{"key1":"value1","key2":"value2","key3":"value3"}
内容现在将应用程序/ JSON
(或至少没有上面提到的),所以PHP的 $ _ POST
-wrapper不知道如何处理(还)。
The content would now be application/json
(or at least none of the above mentioned), so PHP's $_POST
-wrapper doesn't know how to handle that (yet).
中的数据仍然存在,你就无法通过包装访问。所以,你需要自己与的file_get_contents把它拿来RAW格式(PHP://输入')
(只要它不是的multipart / form-data的
克斯codeD )。
The data is still there, you just can't access it through the wrapper. So you need to fetch it yourself in raw format with file_get_contents('php://input')
(as long as it's not multipart/form-data
-encoded).
这也是你将如何访问XML数据或任何其他非标准的内容类型。
This is also how you would access XML-data or any other non-standard content type.
这篇关于PHP" PHP://输入" VS $ _ POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!