file_get_contents 通过get或者post携带的参数,在地址所在的那个方法里获取不到get或post数据

这是post方法请求网址的函数:
function send_post($url, $post_data) {

      $postdata = http_build_query($post_data);    
      $options = array(    
            'http' => array(    
                'method' => 'get',    
                'header' => 'Content-type:application/x-www-form-urlencoded',    
                'content' => $postdata,    
                'timeout' => 15 * 60 // 超时时间(单位:s)    
            )    
        );    
        $context = stream_context_create($options);    
        $result = file_get_contents($url, false, $context);             
        return $result;    
}

调用函数:
$post_data = array(

        'username' => 'username',
        'password' => 'password'
    );
    $html = $this->send_post('http://localhost:8980/newhotel/login/login',$post_data);
    echo $html;
    die;

请求的地址的方法中就写一句话:
echo $_POST;die;
但是浏览器上只输出了一个空array。。。。
这是为什么,get方法也是一样获取不到。。。。。到底怎样才能获取请求地址携带的数据呢?

阅读 6.9k
3 个回答

哎,起初我看你这个代码,感觉除了坑爹的格式外,应该也没啥问题。

随手写了个PHP文件,一执行:

clipboard.png

话说你调试的时候也不开display_errors吗?

那么原因就很简单了:
'method' => 'get', 这个地方指定HTTP METHOD的时候要使用大写的。

比如 'method' => 'GET', , 或者 'method' => 'POST', .

修改后亲测有效。

还是楼上细心.GET这个要大写..

数组怎么用echo 不是用print_r嘛

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题