PHP:可以自动获取所有发布的数据吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6334830/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:03:53  来源:igfitidea点击:

PHP: Possible to automatically get all POSTed data?

phpformspost

提问by Chuck Le Butt

Simple question: Is it possible to get all the data POSTed to a page, even if you don't know all the fields?

简单的问题:即使您不知道所有字段,是否也可以将所有数据发布到页面上?

For example, I want to write a simple script that collects any POSTed data and emails it. I can foresee that the fields in the form are likely to change a lot over time, and so to save myself some time in the long run, I was wondering if I could write something that automatically gathered everything?

例如,我想编写一个简单的脚本来收集任何已发布的数据并将其通过电子邮件发送。我可以预见表单中的字段可能会随着时间的推移而发生很大变化,因此从长远来看,为了节省自己的时间,我想知道是否可以编写一些自动收集所有内容的东西?

Is it possible?

是否可以?

回答by Pekka

Sure. Just walk through the $_POSTarray:

当然。只需遍历$_POST数组:

foreach ($_POST as $key => $value) {
    echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}

回答by Roman Newaza

No one mentioned Raw Post Data, but it's good to know, if posted data has no key, but only value, use Raw Post Data:

没有人提到Raw Post Data,但很高兴知道,如果发布的数据没有键,而只有值,请使用Raw Post Data

$postdata = file_get_contents("php://input");

PHP Man:

PHP人:

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 是一个只读流,允许您从请求正文中读取原始数据。对于 POST 请求,最好使用 php://input 而不是 $HTTP_RAW_POST_DATA,因为它不依赖于特殊的 php.ini 指令。此外,对于默认情况下未填充 $HTTP_RAW_POST_DATA 的那些情况,它是激活 always_populate_raw_post_data 的潜在内存密集型替代方案。php://input 不适用于 enctype="multipart/form-data"。

回答by diEcho

Yes you can use simply

是的,您可以简单地使用

     $input_data = $_POST;

or extract()may be useful for you.

extract()可能对您有用。

回答by George Cummins

All posted data will be in the $_POST superglobal.

所有发布的数据都将在 $_POST 超全局变量中。

http://php.net/manual/reserved.variables.post.php

http://php.net/manual/reserved.variables.post.php

回答by Tickthokk

As long as you don't want any special formatting: yes.

只要您不想要任何特殊格式:是的。

foreach ($_POST as $key => $value) 
    $body .= $key . ' -> ' . $value . '<br>';

Obviously, more formatting would be necessary, however that's the "easy" way. Unless I misunderstood the question.

显然,需要更多的格式,但这是“简单”的方法。除非我误解了这个问题。

You could also do something like this (and if you like the format, it's certainly easier):

你也可以做这样的事情(如果你喜欢这种格式,当然更容易):

$body = print_r($_POST, true);

回答by barfoon

You can retrieve all the keys of the $_POST array using array_keys(), then construct an email messages with the values of those keys.

您可以使用array_keys()检索 $_POST 数组的所有键,然后使用这些键的值构造电子邮件消息。

var_dump($_POST)will also dump information about all of the information in $_POST for you.

var_dump($_POST)还将为您转储有关 $_POST 中所有信息的信息。

回答by searlea

To add to the others, var_exportmight be handy too:

要添加到其他人,var_export也可能很方便:

$email_text = var_export($_POST, true);

回答by Tarik

You can use $_REQUESTas well as $_POSTto reach everything such as Post, Get and Cookie data.

您可以使用$_REQUEST以及$_POST访问所有内容,例如发布、获取和 Cookie 数据。