码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2016-04-15 13:48:56      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

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

总的流程是

技术分享

 

获取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进行

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

标签:

原文地址:http://www.cnblogs.com/tingfengqieyu/p/5395052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!