您好,欢迎访问一九零五行业门户网

微信网页授权传送门代码

给没有公众号的网站实现网页授权登入很不错
where ( $map )->find (); 
        $info=array(
                'appid' => , 
                'appsecret' => ,  
            );
$redirect_url=u('webauth/index');//回调地址
        $code=i('get.code');
        $scope=i('get.scope');
        if(empty($scope)){
            $scope=false;
        }else{
            $scope=true;
        }
        if(empty($code)){ 
            $url='http://ky.xebox.cn/index.php/index/index/index/';//传送地址
            yoauth($info['appid'],$redirect_url,$scope,$url);
        }
        $data=yoauth_access_token($info['appid'],$info['secret'],$code); 
        $canshu=http_build_query($data);  
        echo $url=i('get.state').'?'.$canshu;
        redirect($url);
    } 

/**********************************微信网页授权*****************************************/
/*  第一步:用户同意授权,获取code 
    $appid;//公众号appid
    $redirect_url;//授权后重定向的回调链接地址,请使用urlencode对链接进行处理
    $response_type;//返回类型,请填写code
    $scope;//应用授权作用域,snsapi_base不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
    $state;//重定向后会带上state参数,可以填写公众号id判断用户来自哪个公众号
*/    
function yoauth($appid,$redirect_url,$scope=true,$state='ypwl',$response_type=code){ 
    if($scope){
        $scope='snsapi_userinfo';//弹出网页授权
    }else{
        $scope='snsapi_base';//静默授权
    }
    $map ['appid'] = $appid;
    $map ['redirect_uri'] = $redirect_url;
    $map ['response_type'] = $response_type;
    $map ['scope'] = $scope;
    $map ['state'] = $state;
    $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query ( $map ) . '#wechat_redirect';
    redirect($url); 
}
/*  第二步:通过code换取网页授权access_token 并且获取用户信息省略后面的步骤
    注意:如果是静默方式到这一步就算完成了   直接返回用户的openid snsapi_base式的网页授权流程即到此为止。
    appid    是    公众号的唯一标识
    secret    是    公众号的appsecret
    code    是    填写第一步获取的code参数
    grant_type    是    填写为authorization_code   
 */
function yoauth_access_token($appid,$secret,$code,$grant_type='authorization_code'){ 
    $map ['appid'] = $appid;
    $map ['secret'] = $secret;
    $map ['code'] = $code;
    $map ['grant_type'] = $grant_type; 
    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?'. http_build_query ( $map ) ;
    $content = file_get_contents ( $url );
    $content = json_decode ( $content, true );
    if (! empty ( $content ['errmsg'] )) {
        exit ( $content ['errcode'].'--'.$content['errmsg'] );
    }
    //判断授权方式
    if($content['scope'] == 'snsapi_userinfo'){
        $content=yoauth_userinfo($content['access_token'],$content['openid']);//获取微信用户信息
    }
    return $content;
}
/*
第三步:刷新access_token(如果需要)
 */
/*  第四步:拉取用户信息(需scope为 snsapi_userinfo)
    access_token    网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
    openid    用户的唯一标识
    lang    返回国家地区语言版本,zh_cn 简体,zh_tw 繁体,en 英语
 */
function yoauth_userinfo($access_token,$openid,$lang='zh_cn'){ 
    $map ['access_token'] = $access_token;
    $map ['openid'] = $openid;
    $map ['lang'] = $lang; 
    $url = 'https://api.weixin.qq.com/sns/userinfo?'. http_build_query ( $map );
    $content = file_get_contents ( $url );
    $content = json_decode ( $content, true );
    if (! empty ( $content ['errmsg'] )) {
        exit ( $content ['errcode'].'--'.$content['errmsg'] );
    }
    return $content;

/*  用户openid获取用户信息
    access_token   是   调用接口凭证
    openid  是   普通用户的标识,对当前公众号唯一
    lang    否   返回国家地区语言版本,zh_cn 简体,zh_tw 繁体,en 英语
 */
function yget_wx_userinfo($openid,$access_token,$lang='zh_cn'){
    $map ['access_token'] = $access_token;
    $map ['openid'] = $openid;
    $map ['lang'] = $lang; 
    $url = 'https://api.weixin.qq.com/cgi-bin/user/info?'. http_build_query ( $map );
    $content = file_get_contents ( $url );
    $content = json_decode ( $content, true );
    if (! empty ( $content ['errmsg'] )) {
        exit ( $content ['errcode'].'--'.$content['errmsg'] );
    }
    return $content;
}
其它类似信息

推荐信息