现在的位置: 首页 > 微信开发 > 正文

微信第三方平台授权登陆OAuth 2.0学习

2016年04月17日 微信开发 ⁄ 共 2808字 ⁄ 字号 评论关闭

\

 

微信的项目也做过一两个了,由于主要是负责添加功能,所以关于微信的登陆这一块还挺糊涂,稍微整理一下自己看。

总的流程是

 

 

获取code

 function get_code(){
        code = _GET["code"];
        User = M('User');
        if(_SESSION['uid'] && _SESSION['openid'] && empty(code)){
            member = User->where('uid = '._SESSION['uid'].' and openid ="'._SESSION['openid'].'" ')->find();
            if(empty(member)){
                url = str_replace ( "&" ,  "%26" ,  _SERVER["REQUEST_URI"] );
                header("location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C('WX_APPID')."&redirect_uri=http://"._SERVER['HTTP_HOST'].url."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
            }
        }elseif(!empty(code)){
            userinfo = getUserInfo(code);
            if(userinfo['openid']){
                member = User->where('openid ="'.userinfo['openid'].'" ')->find();
                if(empty(member['openid']) ){
                    User->add(array('wxname'=>userinfo['nickname'],'head_img'=>userinfo['headimgurl'],'openid'=>userinfo['openid'],'regtime'=>time()));
                }else{
                     User->where('openid ="'.userinfo['openid'].'"')->save(array('wxname'=>userinfo['nickname'],'head_img'=>userinfo['headimgurl']));
                }
                member = User->where('openid ="'.userinfo['openid'].'" ')->find();
                session('uid',member['uid']);
                session('openid',member['openid']);
            }
        }else{
            if (!isset(code))
            {
                url = str_replace ( "&" ,  "%26" ,  _SERVER["REQUEST_URI"] );
                header("location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C('WX_APPID')."&redirect_uri=http://"._SERVER['HTTP_HOST'].url."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
            }
        }
    }

 

通过code换取accesstoken

    function getUserInfo(code) {
        appid = C('WX_APPID');
        appsecret = C('WX_APPSECRET');
        //第一步:获取access_token和用户的openid
        access_token = "";
        access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=appsecret&code=code&grant_type=authorization_code";
        access_token_json = https_request(access_token_url);
        access_token_array = json_decode(access_token_json, true);
        access_token = access_token_array['access_token'];
        openid = access_token_array['openid'];
        //第二步:根据用户的openid,读取用户的详细信息
        userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid&lang=zh_CN";
        userinfo_json = https_request(userinfo_url);
        userinfo_array = json_decode(userinfo_json, true);
        return userinfo_array;
    }
    function https_request(url) {
        curl = curl_init();
        curl_setopt(curl, CURLOPT_URL, url);
        curl_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt(curl, CURLOPT_RETURNTRANSFER, 1);
        data = curl_exec(curl);
        if (curl_errno(curl)) {return 'ERROR '.curl_error(curl);}
        curl_close(curl);
        return data;
     }

 

项目是从普通的手机端移植过来的,所以会根据openId来获取uid然后注册session,之后的操作大都使用uid进行

抱歉!评论已关闭.

×