php - file_get_contents 通过get或者post携带的参数,在地址所在的那个方法里获取不到get或post数据
PHPz
PHPz 2017-04-11 10:22:04
[PHP讨论组]

这是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方法也是一样获取不到。。。。。到底怎样才能获取请求地址携带的数据呢?

PHPz
PHPz

学习是最好的投资!

全部回复(3)
PHP中文网

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

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

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

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

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

修改后亲测有效。

天蓬老师

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

高洛峰

数组怎么用echo 不是用print_r嘛

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号