为了账号安全,请及时绑定邮箱和手机立即绑定

var_dump($res);返回NULL是什么原因?

//获取用户的openid

    public function getBaseInfo(){

        //1.获取到code

        $appid = "wxd3fa5caeaf0a1844";

        $redirect_uri=urlencode("http://wx.hodila.com/Index/getUserOpenId");

        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";

        header('location:'.$url);

    }


    public function getUserOpenId(){

        $appid="wxd3fa5caeaf0a1844";

        $appsecret="34cfc7d2f06a3bfdf3165f93d09aa3b0";

        $code=$_GET['code'];

        $url=" https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";

        //拉取用户的openid

        $res=$this->http_curl($url,'get');

        var_dump($res); 

    }

    public function http_curl($url,$type='get',$res='json',$arr=''){

        //1,初始化curl

        $ch = curl_init();

        //2,设置 curl的参数

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        if ($type == 'post') {

            curl_setopt($ch, CURLOPT_POST, $url);

            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);

        }

        //3,采集

        $output = curl_exec($ch);

        //4,关闭

        curl_close($ch);

        if ($res == 'json') {

            if ( curl_errno($ch) ) {

                return curl_error($ch);

            }else{

                return json_decode($output,true);

            }

        }

    }


正在回答

1 回答

确认APPID 和APPsecret 没错哦 把你 function getUserOpenId ()里面的 $url 的https前面的空格删掉 http://img1.sycdn.imooc.com//596500c00001259808810212.jpg

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

var_dump($res);返回NULL是什么原因?

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号