我无法使用 PHP 读取我的 POST HTTP 请求正文!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3362145/
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
I can't read my POST HTTP request's body with PHP !
提问by Dirty Henry
I've never used PHP but right now, I need to write a PHP file that displays in a log file the content of the body of a POST HTTP request.
我从未使用过 PHP,但现在,我需要编写一个 PHP 文件,在日志文件中显示 POST HTTP 请求正文的内容。
I've read that you can access variables of the body via the _POST array. Unfortunately, it seems to be empty, although I'm pretty sure there is stuff in my HTTP request's body !
我读过您可以通过 _POST 数组访问主体的变量。不幸的是,它似乎是空的,尽管我很确定我的 HTTP 请求正文中有内容!
What should I use to be 100% sure of the content of my HTTP body ?
我应该使用什么来 100% 确定我的 HTTP 正文的内容?
Thanks.
谢谢。
回答by salathe
$post_body = file_get_contents('php://input');
php://inputallows you to read raw POST data. It is a less memory intensive alternative to$HTTP_RAW_POST_DATAand does not need any special php.ini directives.php://inputis not available with enctype="multipart/form-data".
php://input允许您读取原始 POST 数据。它是一种内存密集度较低的替代品,$HTTP_RAW_POST_DATA不需要任何特殊的 php.ini 指令。php://input不适用于 enctype="multipart/form-data"。
(Source: http://php.net/wrappers.php)
(来源:http: //php.net/wrappers.php)
回答by Anax
The global variable is $_POST, not _POST. Also it might be that you are sending the data via GETmethod, in which case you need to use the $_GETglobal variable.
全局变量是$_POST,不是_POST。也可能是您通过GET方法发送数据,在这种情况下,您需要使用$_GET全局变量。
If you want to check for either POSTor GETmethod, you can use the global variable $_REQUEST. Sample code bellow:
如果要检查POST或GET方法,可以使用全局变量$_REQUEST。示例代码如下:
<html>
<body>
<form method="POST" action="postdata.php">
<input type="text" name="mydata" />
<input type="submit">
</form>
</body>
</html>
file postdata.php:
文件postdata.php:
<?php
$result = $_POST['mydata'];
echo $result;
回答by middus
Maybe you misspelled it. The array's correct name $_POST.
也许你拼错了。数组的正确名称$_POST。
Try this
尝试这个
<?php
var_dump($_POST);
and see what happens.
看看会发生什么。

