POST、DELETE 不通过stream_context_create() / fopen() 发送响应
我有以下代码,用于通过 HTTP 发布数据
$opts = array (
'http' => array (
'method' => "POST",
'header' => $auth ,
// . "Content-Type: " . $content_type . "\r\n"
// . "Content-length: " . strlen($data) . "\r\n",
'user_agent' => RESTClient :: USER_AGENT,
'content' => $data
)
);
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
$result = "";
while ($str = fread($fp,1024)) {
$result .= $str;
}
fclose($fp);
Return $result;
当我在数据库中查看时,发布的数据已被输入,但是,我的代码应该返回响应。当我使用 cURL 但不是通过此方法时,响应可以正常工作。我错过了什么吗?
I have the following code that I use to post data via HTTP
$opts = array (
'http' => array (
'method' => "POST",
'header' => $auth ,
// . "Content-Type: " . $content_type . "\r\n"
// . "Content-length: " . strlen($data) . "\r\n",
'user_agent' => RESTClient :: USER_AGENT,
'content' => $data
)
);
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
$result = "";
while ($str = fread($fp,1024)) {
$result .= $str;
}
fclose($fp);
Return $result;
It is posting as when I look in the database the the posted data has been entered, however, my code shouldbe bringing back a response. The responses work correctly when I use cURL but not via this method. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了由于缺少内容类型而出现的警告/通知之外,这
对我来说效果很好。
此代码片段是否会在系统上产生所描述的行为?
顺便说一句:除了 fopen/while/fread/fclose 之外,您还可以使用 file_get_contents。
Except for a warning/notice because of the missing Content-type this
works fine for me.
Does this code snippet produce the described behaviour on system?
btw: instead of fopen/while/fread/fclose you can also use file_get_contents.