POST、DELETE 不通过stream_context_create() / fopen() 发送响应

发布于 2024-12-12 10:28:28 字数 823 浏览 0 评论 0原文

我有以下代码,用于通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2024-12-19 10:28:28

除了由于缺少内容类型而出现的警告/通知之外,这

<?php
class RESTClient {
    const USER_AGENT='Mozilla';
}
$url = 'http://social.msdn.microsoft.com/Search/en-US/';
$data = 'query=test&ac=3';
$auth = '';

// <-- unaltered code
$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);
//  unaltered code -->
echo $result;

对我来说效果很好。
此代码片段是否会在系统上产生所描述的行为?


顺便说一句:除了 fopen/while/fread/fclose 之外,您还可以使用 file_get_contents

Except for a warning/notice because of the missing Content-type this

<?php
class RESTClient {
    const USER_AGENT='Mozilla';
}
$url = 'http://social.msdn.microsoft.com/Search/en-US/';
$data = 'query=test&ac=3';
$auth = '';

// <-- unaltered code
$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);
//  unaltered code -->
echo $result;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文