如何在 php 中获取 POST 的正文?
我将以下内容作为 POST 提交到 php 页面:
I submit as POST to a php page the following:
{a:1}
这是请求的主体(POST 请求).
在 php 中,我该怎么做才能提取该值?
This is the body of the request (a POST request).
In php, what do I have to do to extract that value?
var_dump($_POST);
不是解决方案,不起作用.
is not the solution, not working.
推荐答案
访问 POST 或 PUT 请求(或任何其他 HTTP 方法)的实体主体:
To access the entity body of a POST or PUT request (or any other HTTP method):
$entityBody = file_get_contents('php://input');
另外,STDIN
常量是一个已经打开的 php://input
流,所以你也可以这样做:
Also, the STDIN
constant is an already-open stream to php://input
, so you can alternatively do:
$entityBody = stream_get_contents(STDIN);
来自 关于 I/O 流的 PHP 手动条目文档:
php://input 是一个只读流,允许您读取原始数据从请求正文.在 POST 请求的情况下,最好使用 php://input 而不是 $HTTP_RAW_POST_DATA
因为它没有依赖于特殊的 php.ini 指令.此外,对于那些情况$HTTP_RAW_POST_DATA
默认不填充,它是一个潜在的激活时内存占用较少的替代方案always_populate_raw_post_data.php://input 不适用于enctype="multipart/form-data".
php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of
$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".
您需要特别注意的是,php://input
流,无论您如何在 Web SAPI 中访问它,都是不可搜索的.这意味着它只能被读取一次.如果您在经常上传大型 HTTP 实体主体的环境中工作,您可能希望以流形式维护输入(而不是像上面的第一个示例那样缓冲它).
Specifically you'll want to note that the php://input
stream, regardless of how you access it in a web SAPI, is not seekable. This means that it can only be read once. If you're working in an environment where large HTTP entity bodies are routinely uploaded you may wish to maintain the input in its stream form (rather than buffering it like the first example above).
为了维护流资源,这样的事情可能会有所帮助:
To maintain the stream resource something like this can be helpful:
<?php
function detectRequestBody() {
$rawInput = fopen('php://input', 'r');
$tempStream = fopen('php://temp', 'r+');
stream_copy_to_stream($rawInput, $tempStream);
rewind($tempStream);
return $tempStream;
}
php://temp
允许您管理内存消耗,因为它会在存储一定数量的数据(默认为 2M)后透明地切换到文件系统存储.可以在 php.ini 文件中或通过附加 /maxmemory:NN
来操作此大小,其中 NN
是在使用临时文件之前要保留在内存中的最大数据量, 以字节为单位.
php://temp
allows you to manage memory consumption because it will transparently switch to filesystem storage after a certain amount of data is stored (2M by default). This size can be manipulated in the php.ini file or by appending /maxmemory:NN
, where NN
is the maximum amount of data to keep in memory before using a temporary file, in bytes.
当然,除非您有充分的理由在输入流上进行搜索,否则您不应该在 Web 应用程序中需要此功能.读取 HTTP 请求实体正文一次通常就足够了——不要让客户端整天等待,而您的应用正在弄清楚该做什么.
Of course, unless you have a really good reason for seeking on the input stream, you shouldn't need this functionality in a web application. Reading the HTTP request entity body once is usually enough -- don't keep clients waiting all day while your app figures out what to do.
请注意,php://input 不适用于指定 Content-Type: multipart/form-data
标头(enctype="multipart/form-data"
在 HTML 表单中).这是因为 PHP 已经将表单数据解析为 $_POST
超全局变量.
Note that php://input is not available for requests specifying a Content-Type: multipart/form-data
header (enctype="multipart/form-data"
in HTML forms). This results from PHP already having parsed the form data into the $_POST
superglobal.
相关文章