加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何使用cURL发送原始POST数据? (PHP)

发布时间:2020-12-13 23:07:01 所属栏目:Linux 来源:网络整理
导读:我正在尝试使用$HTTP_RAW_POST_DATA将原始POST数据发送到页面,但我的尝试失败并且给出了未定义的索引通知. 我尝试过以下方法: curl_setopt($handle,CURLOPT_POSTFIELDS,'Raw POST data'); // Doesn't seem to work at all.curl_setopt($handle,array('Raw P
我正在尝试使用$HTTP_RAW_POST_DATA将原始POST数据发送到页面,但我的尝试失败并且给出了未定义的索引通知.

我尝试过以下方法:

curl_setopt($handle,CURLOPT_POSTFIELDS,'Raw POST data');   // Doesn't seem to work at all.
curl_setopt($handle,array('Raw POST data'));   // 0 => Raw POST data

我做了一些研究,有些人建议在请求中发送一个标题(Content-Type:text / plain),这似乎没有任何影响.

如果有人能够为这个问题提供解决方案,我将非常感激.谢谢!

解决方法

您的发送方/接收方周期的响应部分出现错误.

虽然这很可能是发送方的问题(它没有发送正确的请求),但也可能是由接收PHP脚本中的配置错误引起的.也就是说,即使请求是正确的,接收器也可能没有HTTP_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”.

因此,首先要检查的是是否确实填充了$HTTP_RAW_POST_DATA,从上面的页面要求:

> .ini变量always_populate_raw_post_data为True,
>或POST与POST处理程序无法识别的Content-Type一起发送(也许可以使用“text / raw”)

此时,正确的发送数据的方式将是

curl_setopt($handle,urlencode('Raw POST data'));

但是,请注意,接收这些数据的推荐方法根本不是依赖于$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_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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读