1.登陆的html页面代码

<a href="weixin.redirect_uri">微信登陆</a>

2.跳转到的控制器方法代码

CJUL是常量,就是你在开放平台上注册的重定向网址

跳换到后重定向到的地址是微信的一个重定向地址

https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

带了你在开放平台上注册的网址redirect_uri和appid 并且scope为snsapi_login

参数 是否必须 说明
appid 应用唯一标识
redirect_uri 重定向地址,需要进行UrlEncode
response_type 填code
scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可
state 用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验

class  PhoneController extends CommonController{    

        //加载首页
        public function Index(){

            //通过授权跳转网页进入
            if(isset($_GET['code'])){

                $code = $_GET['code'];

                //通过code换取网页授权access_token
                $access_token = $this->get_code_access_token($code);

                //检验授权凭证(access_token)是否有效
                $data = $this->checkAvail($access_token['access_token'],$access_token['openid']);

                if($data['errcode'] != '0' || $data['error_msg'] != 'ok'){
                    
                    //刷新access_token
                    $access_token = $this->refresh_access_token($access_token['refresh_token']);
                
                }

                //得到拉取用户信息(需scope为 snsapi_userinfo)
                $userinfo = $this->get_user_info($access_token['access_token'],$access_token['openid']);

                require 'view/index.html';

            }elseif(isset($_GET['page'])){

                //网页端入口
                require 'view/index.html';
                

            }else{

                echo "NO CODE";
            }



        }

               //跳转到微信的认证页
        public function redirect_uri(){
            $url =  $this->get_authorize_url(CJURL,'1');
             
            header('location:'.$url);
        }
}

3.调用的底层代码

<?php
    
    class CommonController{

        //http get
        public function httpRequest($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);

            if($output == false){
                return 'curl error:'.curl_error($ch);
            }

            curl_close($ch);

            return $output;
        }


        //curl获取参数
        //https 中的 get  和  post
        public function https_request($url,$data=null){
            
            $curl = curl_init();
        
            curl_setopt($curl,CURLOPT_URL,$url);

            curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);

            //不为空,使用post传参数,否则使用get
            if($data){

                curl_setopt($curl,CURLOPT_POST,1);
                curl_setopt($curl,CURLOPT_POSTFIELDS,$data);

            }

            curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
            
            $output = curl_exec($curl);
            curl_close($curl);

            return $output;
        }

        //获取接口票据access_token
        public function get_token(){

            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&app,$result);
        }

    }

 ?>

相关文章:

  • 2021-07-24
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-06-14
  • 2021-11-04
  • 2021-11-24
  • 2021-06-05
猜你喜欢
  • 2021-11-21
  • 2021-11-21
  • 2021-07-10
  • 2021-09-21
  • 2021-09-17
  • 2021-08-25
相关资源
相似解决方案