扫码登录/微信游览器授权登录




    if (isMobile()){
//微信内
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=地址&response_type=code&scope=snsapi_userinfo&state=a-zA-Z0-9&connect_redirect=1#wechat_redirect"  class="fa fa-weixin"  title="微信登录"></a>
                header('Content-Type: text/html; charset=utf-8');           
                $code = input('code');
                $state = input('state');
                $appid='';
                $appsecret='';
                $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
                $token = getJson($token_url);    
                $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token['refresh_token'];
                        //转成对象
                $access_token = getJson($access_token_url);
                $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token['access_token'].'&openid='.$access_token['openid'].'&lang=zh_CN';
                $user_info_url=iconv('ISO-8859-1','UTF-8',$user_info_url);
                $user_info = getJson($user_info_url);
                $rs =  json_decode(json_encode($user_info),true);//返回的json数组转换成array数组
                 session('openids',$rs['openid']);
                $data=array(
                    'openid'=> $rs['openid'],
                    'wx_username'=>$rs['nickname'],
                    'wx_img'=>$rs['headimgurl'],
                    'wx_dz'=>$rs['country'].$rs['province'].$rs['city'],
                    'unionid'=>$rs['unionid'],
                );  
                $b=db('wx_users')->where('unionid',$data['unionid'])->find();   
                if ($data['unionid']!=$b['unionid']) {
                $a = db('wx_users')->insert($data);
                }    
                $c=db('wx_users')->where('unionid',$data['unionid'])->find();  

                session('wx',$c);
                $this->redirect('home/index/index');    
             }else{

          //pc扫码登录

 <a href=" https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=地址&response_type=code&scope=snsapi_login&state=生成STATE#wechat_redirect"  class="fa fa-weixin"  title="微信登录"></a>
                    header('Content-Type: text/html; charset=utf-8');                    
                    ini_set('allow_url_fopen','On');
                    ini_set('allow_url_include','On');   
                    $code = input('code');
                    $state = input('state');
                    //换成自己的接口信息
                    $appid='wxe0e445cbb8d16f05';
                    $appsecret='22b2b77103402134d5ccbaef8f5b06f5';
                    if (empty($code)) $this->error('授权失败');
                    $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

                    $token = getJson($token_url);

                    if (isset($token['errcode'])) {
                    echo '<h1>错误:</h1>'.$token['errcode'];
                    echo '<br/><h2>错误信息:</h2>'.$token['errmsg'];
                    exit;
                    }
                    $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token['refresh_token'];
                    //转成对象
                    $access_token = getJson($access_token_url);
                    if (isset($access_token->errcode)) {
                    echo '<h1>错误:</h1>'.$access_token->errcode;
                    echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;
                    exit;
                    }
                    $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token['access_token'].'&openid='.$access_token['openid'].'&lang=zh_CN';
                    $user_info_url=iconv('ISO-8859-1','UTF-8',$user_info_url);
                    //转成对象
                    $user_info = getJson($user_info_url);
                    if (isset($user_info->errcode)) {
                    echo '<h1>错误:</h1>'.$user_info->errcode;
                    echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;
                    exit;
                    }

                    $rs =  json_decode(json_encode($user_info),true);//返回的json数组转换成array数组
                    
                    //打印用户信息
                    $data=array(
                    'openid'=> $rs['openid'],
                    'wx_username'=>$rs['nickname'],
                    'wx_img'=>$rs['headimgurl'],
                    'wx_dz'=>$rs['country'].$rs['province'].$rs['city'],
                    'unionid'=>$rs['unionid'],
                );  
                $b=db('wx_users')->where('unionid',$data['unionid'])->find();   
                if ($data['unionid']!=$b['unionid']) {
                $a = db('wx_users')->insert($data);
                }    
                $c=db('wx_users')->where('unionid',$data['unionid'])->find();  

                    session('wx',$c);
                    $this->redirect('home/index/index');    

           }  




// curl转数组
 function getJson($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);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
 }



此处评论已关闭

[player id="719102"/]