如何使用 cURL 发送原始 POST 数据?(PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13099177/
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 send raw POST data with cURL? (PHP)
提问by user1488335
I'm attempting to send raw POST data to a page using $HTTP_RAW_POST_DATA, but my attempts failed and an undefined index notice was given.
我正在尝试使用$HTTP_RAW_POST_DATA将原始 POST 数据发送到页面,但我的尝试失败并给出了未定义的索引通知。
I have attempted the following:
我尝试了以下方法:
curl_setopt($handle, CURLOPT_POSTFIELDS, 'Raw POST data'); // Doesn't seem to work at all.
curl_setopt($handle, CURLOPT_POSTFIELDS, array('Raw POST data')); // 0 => Raw POST data
I did some research and some people had suggested sending a header (Content-Type: text/plain) in the request, which didn't seem to affect anything.
我做了一些研究,有些人建议在请求中发送一个标头(Content-Type: text/plain),这似乎没有任何影响。
If anyone is able to provide a solution for this issue I would greatly appreciate it. Thank you!
如果有人能够为此问题提供解决方案,我将不胜感激。谢谢!
采纳答案by user1488335
For some odd reason the aforementioned header thing seems to fix it, previously it wasn't working, I'm unsure why it works now. Anyway, for those who don't know this is the code:
出于某种奇怪的原因,前面提到的标题似乎修复了它,以前它不起作用,我不确定为什么它现在起作用了。无论如何,对于那些不知道这是代码的人:
curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
回答by LSerni
You get an error in the responsepart of your sender/receiver cycle.
您在发送方/接收方周期的响应部分出现错误。
While this might very well be a problem of the sender(which does not send a proper request), it may also be caused by a misconfiguration in the receivingPHP script. To wit, even if the request is correct, the receiver might nonetheless not have HTTP_RAW_POST_DATAavailable.
虽然这很可能是发送方的问题(它没有发送正确的请求),但也可能是由接收PHP 脚本中的错误配置引起的。也就是说,即使请求是正确的,接收者也可能没有HTTP_RAW_POST_DATA可用的信息。
See: http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
请参阅:http: //www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
Always populate the $HTTP_RAW_POST_DATA containing the raw POST data. Otherwise, the variable is populated only with unrecognized MIME type of the data. However, the preferred method for accessing the raw POST data is php://input. $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data".
始终填充包含原始 POST 数据的 $HTTP_RAW_POST_DATA。否则,该变量仅填充无法识别的 MIME 类型的数据。但是,访问原始 POST 数据的首选方法是 php://input。$HTTP_RAW_POST_DATA 不适用于 enctype="multipart/form-data"。
So the first thing to check is whether $HTTP_RAW_POST_DATAis indeed populated, which from the page above requires either:
所以首先要检查的是是否$HTTP_RAW_POST_DATA确实填充了,从上面的页面需要:
- the .ini variable
always_populate_raw_post_datais True, - or the POST is sent with a
Content-Typethat the POST handler does not recognize (maybe one could use "text/raw")
- .ini 变量
always_populate_raw_post_data为 True, - 或者 POST 发送
Content-Type的 POST 处理程序无法识别(也许可以使用“文本/原始”)
At this point, the correct way of sending data would be
此时,正确的数据发送方式是
curl_setopt($handle, CURLOPT_POSTFIELDS, urlencode('Raw POST data'));
However, note that the recommended way of receiving those datawould be not to rely on $HTTP_RAW_POST_DATAat all, but to read the contents of the virtual file php://input.
但是,请注意,推荐的接收这些数据的方式不是完全依赖$HTTP_RAW_POST_DATA,而是读取虚拟文件的内容php://input。
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_DATAas 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 是一个只读流,允许您从请求正文中读取原始数据。在 POST 请求的情况下,最好使用 php://input 而不是
$HTTP_RAW_POST_DATA因为它不依赖于特殊的 php.ini 指令。此外,对于默认情况下未填充 $HTTP_RAW_POST_DATA 的那些情况,它是激活 always_populate_raw_post_data 的潜在内存密集型替代方案。

