微信登录oauth2.0

微信oauth2.0登陆流程:

  1. 客户端(User)访问站点
  2. Web站点跳转至授权服务器,并带上appidredirect_uri、response_typescopestate等参数
  3. 授权服务器请求用户输入账号和密码;
  4. 用户输入账号和密码,并授权;
  5. 授权服务器返回codeWeb站点
  6. Web站点带上appidappsecretcodegrant_type参数返回给授权服务器,请求获取access_tokenopenid  ;
  7. 授权服务器返回access_tokenopenidrefresh_token等参数;
  8. Web站点带上access_tokenopenidlang参数给授权服务器获取用户信息;
  9. 授权服务器返回用户信息给Web站点;
  10. Web站点将用户信息返回给客户端(User.

 

流程图:

 

PHP源码(APPID 和 APPSECRET 换成自己申请的):

<?php

/*
 * 绑定微信 bobwen add
 */
define("APPID", "wwacs354wertf89iykgk");
define("APPSECRET", "9125435wertsfgsdg7f08edsgas0ae8");
class weixin{
    var $appid = APPID;
    var $appsecret = APPSECRET;
    var $access_token = '';
    /**
     * 获取openid
     */
    function get_openid() {
        if (!empty($appid))
        {

            $openid=@$_COOKIE['sopenid'];
            if(array_key_exists('HTTP_USER_AGENT', $_SERVER)){
                if(!$openid && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){         
                    //使用jsapi接口
                    //$jsApi = new JsApi_pub();
                    if (!isset($_GET['code']))
                    {
                        //=========步骤1:网页授权获取用户openid============
                        //通过code获得openid

                        //触发微信返回code码
                        $backurl = $this->get_url();
                        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=".urlencode($backurl)."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
                        //echo $url;
                        Header("Location: $url");
                    }else
                    {
                        //获取code码,以获取openid
                        $code = $_GET['code'];

                        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=".$code."&grant_type=authorization_code";
                        $re = $this->http_request_json($url);
                        $rearr = json_decode($re,true);//获取返回的appid和access_token
                        if(isset($rearr['openid']))
                        {
                            $openid = $rearr['openid'];

                            setcookie("sopenid",$openid,time()+864000,'/');
                        }
                    }
                }
            }
        }
        return $rearr;
    }

    /**
     * 获取用戶信息
     */
    function get_user_info($res) {
        $url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$res['access_token']&openid=$res['sopenid']";
        $re = $this->http_request_json($url);
        $rearr = json_decode($re,true);
        return $rearr;
    }

    /**
     * 获取当前页面完整URL地址
     */
    function get_url() {
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
        $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
        $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
        $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
        return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }
    function http_request_json($url){    
       $ch = curl_init();  
       curl_setopt($ch, CURLOPT_URL,$url);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
       $result = curl_exec($ch);  
       curl_close($ch);  
       return $result;    
    }    
}
$weixin = new weixin();
$res=$weixin->get_openid();
$usrinfo=$weixin->get_user_info($res);
return $userinfo;
?>

posted on 2016-06-14 09:09  陶潜别梦  阅读(585)  评论(0编辑  收藏  举报