php 如何在php中获取POST的正文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8945879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get body of a POST in php?
提问by Itay Moav -Malimovka
I submit as POST to a php page the following:
我将以下内容作为 POST 提交到 php 页面:
{a:1}
This is the body of the request (a POST request).
In php, what do I have to do to extract that value?
这是请求的主体(POST 请求)。
在 php 中,我该怎么做才能提取该值?
var_dump($_POST);
is not the solution, not working.
不是解决方案,不起作用。
回答by rdlowrey
To access the entity body of a POST or PUT request (or any other HTTP method):
要访问 POST 或 PUT 请求(或任何其他 HTTP 方法)的实体主体:
$entityBody = file_get_contents('php://input');
Also, the STDIN
constant is an already-open stream to php://input
, so you can alternatively do:
此外,STDIN
常量是一个已经打开的流php://input
,因此您可以选择执行以下操作:
$entityBody = stream_get_contents(STDIN);
From the PHP manual entry on I/O streamsdocs:
php://inputis 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://inputinstead 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://inputis not available with enctype="multipart/form-data".
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"。
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).
具体来说,您需要注意php://input
流,无论您如何在 Web SAPI 中访问它,都是不可搜索的。这意味着它只能被读取一次。如果您在一个经常上传大型 HTTP 实体主体的环境中工作,您可能希望以流形式维护输入(而不是像上面的第一个示例那样缓冲它)。
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
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.
php://temp
允许您管理内存消耗,因为它会在存储一定数量的数据(默认为 2M)后透明地切换到文件系统存储。这个大小可以在 php.ini 文件中操作,也可以通过附加/maxmemory:NN
,其中NN
是在使用临时文件之前保留在内存中的最大数据量,以字节为单位。
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.
当然,除非您有充分的理由在输入流上查找,否则您不应该在 Web 应用程序中需要此功能。读取 HTTP 请求实体主体一次通常就足够了——不要让客户端在你的应用程序弄清楚要做什么时整天等待。
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://input 不适用于指定Content-Type: multipart/form-data
标题(enctype="multipart/form-data"
在 HTML 表单中)的请求。这是因为 PHP 已经将表单数据解析为$_POST
超全局变量。
回答by umesh bhanderi
return value in array
数组中的返回值
$data = json_decode(file_get_contents('php://input'), true);
回答by Legolas
A possible reason for an empty $_POST
is that the request is not POST
, or not POST
anymore... It may have started out as post, but encountered a 301
or 302
redirect somewhere, which is switched to GET
!
空的一个可能原因$_POST
是请求不是POST
,或者POST
不再是......它可能以 post 开始,但在某处遇到 a 301
or302
重定向,它被切换到GET
!
Inspect $_SERVER['REQUEST_METHOD']
to check if this is the case.
检查$_SERVER['REQUEST_METHOD']
以检查是否是这种情况。
See https://stackoverflow.com/a/19422232/109787for a good discussion of why this should not happen but still does.
请参阅https://stackoverflow.com/a/19422232/109787以详细讨论为什么这不应该发生但仍然发生。
回答by linepogl
Check the $HTTP_RAW_POST_DATA
variable
回答by shivanshu patel
If you have installed HTTP PECL extension, you can make use of the http_get_request_body()
function to get body data as a string.
如果您安装了 HTTP PECL 扩展,您可以使用该http_get_request_body()
函数以字符串形式获取正文数据。
回答by spinkus
回答by Hatzegopteryx
function getPost()
{
if(!empty($_POST))
{
// when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request
// NOTE: if this is the case and $_POST is empty, check the variables_order in php.ini! - it must contain the letter P
return $_POST;
}
// when using application/json as the HTTP Content-Type in the request
$post = json_decode(file_get_contents('php://input'), true);
if(json_last_error() == JSON_ERROR_NONE)
{
return $post;
}
return [];
}
print_r(getPost());
回答by Yehuda Schwartz
http_get_request_body()
was explicitly made for getting the body of PUT
and POST
requests as per the documentation http://php.net/manual/fa/function.http-get-request-body.php
http_get_request_body()
根据文档http://php.net/manual/fa/function.http-get-request-body.php明确获取PUT
和POST
请求的主体