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

查看:524
本文介绍了php - 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方法也是一样获取不到。。。。。到底怎样才能获取请求地址携带的数据呢?

解决方案

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

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

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

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

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

修改后亲测有效。

这篇关于php - file_get_contents 通过get或者post携带的参数,在地址所在的那个方法里获取不到get或post数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆