php 获取原始帖子数据

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

Get raw post data

phprequest

提问by Andrey M.

According to php manual nor php://input neither $HTTP_RAW_POST_DATAwork with multipart/form-dataPOST-requests.

根据 php 手册和 php://input 都不$HTTP_RAW_POST_DATA适用于multipart/form-dataPOST 请求。

"php://input allows 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://input is not available with enctype="multipart/form-data"."

“php://input 允许您读取原始 POST 数据。它是一种内存占用较少的替代方案,$HTTP_RAW_POST_DATA不需要任何特殊的 php.ini 指令。php://input 不可用enctype="multipart/form-data"。”

How can I get raw data for multipart/form-dataforms?

如何获取multipart/form-data表单的原始数据?

回答by Anti Veeranna

Direct answer: you can not do that. PHP insists on parsing it itself, whenever it sees the multipart/form-data Content-Type. The raw data will not be available to you. Sadly. But you can hack around it.

直接回答:你不能那样做。PHP 坚持自己解析它,只要它看到 multipart/form-data Content-Type。您将无法获得原始数据。可悲。但是你可以绕过它。

I hit a similar problem, a partner was sending incorrectly formatted data as multipart/form-data, PHP could not parse it and was not giving it out so I could parse it myself.

我遇到了类似的问题,合作伙伴将格式不正确的数据作为 multipart/form-data 发送,PHP 无法解析它并且没有给出它,所以我可以自己解析它。

The solution? I added this to my apache conf:

解决方案?我将此添加到我的 apache conf 中:

<Location "/backend/XXX.php">
    SetEnvIf Content-Type ^(multipart/form-data)(.*) NEW_CONTENT_TYPE=multipart/form-data-alternate OLD_CONTENT_TYPE=
    RequestHeader set Content-Type %{NEW_CONTENT_TYPE}e env=NEW_CONTENT_TYPE
</Location> 

This will change the Content-Type of incoming request to XXX.php from multipart/form-data to multipart/form-data-alternate, which is enough to block PHP from trying to parse it

这会将传入请求的 Content-Type 更改为 XXX.php,从 multipart/form-data 更改为 multipart/form-data-alternate,这足以阻止 PHP 尝试解析它

After this you can finally read the whole raw data from php://input and parse it yourself.

在此之后,您终于可以从 php://input 读取整个原始数据并自己解析它。

It is ugly, but I have not found a better or in fact any other solution - short of asking the partner to fix their side.

这很丑陋,但我还没有找到更好的或实际上任何其他解决方案 - 没有要求合作伙伴修复他们的一面。

NB! When you do what I described here, $_FILES will be empty.

注意!当您执行我在此处描述的操作时, $_FILES 将为空。

回答by gerard

You can set enable_post_data_reading = Offand PHP won't intercept multipart/form-datadata.

您可以设置enable_post_data_reading = Off,PHP 不会拦截multipart/form-data数据。

Requires: PHP 5.4

要求:PHP 5.4

回答by Joey Hewitt

I didn't implement this fully, but it looks like it should work. In Apache conf:

我没有完全实现这一点,但看起来它应该可以工作。在Apache conf

SetEnvIf Content-Type ^(multipart/form-data)(.*) MULTIPART_CTYPE=
RequestHeader set Content-Type application/x-httpd-php env=MULTIPART_CTYPE
RequestHeader set X-Real-Content-Type %{MULTIPART_CTYPE}e env=MULTIPART_CTYPE

Setting the Content-Typeto application/x-httpd-phpappears to solve the original problem of PHP parsing the body, and the problem Norbert Farkasreported: "Apache sends back PHP source code". The body is then available on php://input, and the real content type in the X-Real-Content-Typeheader. (That header may not be necessary for you -- the MULTIPART_CTYPEvariable didn't seem to be showing up in my $_ENV, but the new header did.) All other requests should be handled as usual.

设置Content-Typeapplication/x-httpd-php似乎解决了 PHP 解析正文的原始问题,以及Norbert Farkas报告的问题:“Apache 发回 PHP 源代码”。然后可以在 上使用正文php://input,并在X-Real-Content-Type标题中使用真实的内容类型。(该标头对您来说可能不是必需的——MULTIPART_CTYPE变量似乎没有出现在 my 中$_ENV,但新标头却出现了。)所有其他请求都应照常处理。

Thanks to Anti Veerannafor most of it! :)

感谢Anti Veeranna的大部分工作!:)

EDIT: P.S. Obviously it's Apache-specific, but in some of the other configurations of PHP there may very well be easier ways.

编辑: PS 显然它是特定于 Apache 的,但是在 PHP 的其他一些配置中,很可能有更简单的方法。