POST, DELETE没有通过stream_context_create() / fopen()发送响应


POST, DELETE not sending response via 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时,响应工作正确,但不是通过这个方法。我错过什么了吗?

除了一个警告/通知,因为缺少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;

很适合我。
这个代码片段是否在系统上产生所描述的行为?


btw:除了fopen/while/fread/fclose,你还可以使用file_get_contents.