如何在php中获取POST的正文? [英] How to get body of a POST in php?

查看:57
本文介绍了如何在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手动输入 docs :

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".

特别要注意的是,无论您如何在Web SAPI中访问php://input流,都是不可搜索的.这意味着它只能被读取一次.如果您在定期上传大型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.

请注意,对于指定Content-Type: multipart/form-data标头(HTML格式为enctype="multipart/form-data")的请求,php://input不可用.这是由于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.

这篇关于如何在php中获取POST的正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆