zoukankan      html  css  js  c++  java
  • 微信扫描登陆

    过程:点击页面上的微信登陆,打开一个弹出,弹出的内容是一个网页,网页的地址是public function wxlogin()这个控制器的地址,用户扫码之后会回调到 wxlogincallback()方法,获取用户信息

    ///////////////////////////////测试微信登录//////////////////////////////////////////
    public function wxlogin(){
    //-------配置
    $AppID = 'wxeaa7115dff4fc477';
    $AppSecret = '3ece6e0b82662d31018ec959300c3d37';
    $callback = 'http://zyzl.wolfmedia.cn/index/Index/wxlogincallback'; //回调地址
    //$callback = 'http://toutiao.weidong100.com/index/Index/wxlogincallback'; //回调地址
    //微信登录
    $state = md5(uniqid(rand(), TRUE));
    $_SESSION["wx_state"] = $state;
    $callback = urlencode($callback);
    $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$AppID."&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
    header("Location: $wxurl");
    }

    //回调
    public function wxlogincallback(){
    if($_GET['state']!=$_SESSION["wx_state"]){
    exit("5001");
    }
    $AppID = 'wxeaa7115dff4fc477';
    $AppSecret = '3ece6e0b82662d31018ec959300c3d37';
    $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $json = curl_exec($ch);
    curl_close($ch);
    $arr=json_decode($json,1);
    //得到 access_token 与 openid
    $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $json = curl_exec($ch);
    curl_close($ch);
    $arr=json_decode($json,1);
    //得到 用户资料
    print_r($arr);

    }

  • 相关阅读:
    eslint 翻译
    .prettierrc
    轻松掌握golang的defer机制
    golang的逃逸分析
    《python解释器源码剖析》第17章--python的内存管理与垃圾回收
    《python解释器源码剖析》第16章--python的多线程机制
    《python解释器源码剖析》第15章--python模块的动态加载机制
    MongoDB:最受欢迎的文档型数据库
    (持续更新中)opencv:处理图像的高性能模块
    (写了一点写不下去了,虽然垃圾但又不想扔)nginx--最流行的web服务器
  • 原文地址:https://www.cnblogs.com/tingfengqieyu/p/5833745.html
Copyright © 2011-2022 走看看