zoukankan      html  css  js  c++  java
  • 微信 之网页第三方微信扫码登录

    一、方式一(网站内嵌二维码微信登录JS)

    <?php
    /**
     * Created by PhpStorm.
     * User: 25754
     * Date: 2019/6/4
     * Time: 11:15
     */
    
    $state = md5("yangs");
    
    include './login.html';
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            #login_container{
                position: absolute;
                left: 50%;
                margin-left: -150px;
            }
        </style>
    </head>
    <body>
    <div id="login_container"></div>
    <script type="text/javascript" src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
    <script>
        var obj = new WxLogin({
            self_redirect:true,
            id:"login_container",
            appid: "wx1851214902ef11bb",
            scope: "snsapi_login",
            redirect_uri: encodeURIComponent("http://www.boyuan.com/api/a/wx_third_login/login_third_weixin.php?action=do"),
            state: "{$state}",
            style: "",
            href: ""
        });
    </script>
    </body>
    </html>

    结果如图:

    二、方式二(获取access_token)

    <a href="/api/a/wx_third_login/login_third_weixin.php?action=init" target="_blank" class="wx" style="float:left"><i style="top:0;"></i>微信账号授权登录</a>
    require DT_ROOT . '/api/a/wxopen.class.php';
    
    $wx = new WXOpen();
    $state = md5("yangs");
    switch ($action) {
        case 'init':
            $redirect_uri = "http://www.boyuan.com/api/a/wx_third_login/login_third_weixin.php?action=do";
            $scope = 'snsapi_login';
            $url = $wx->qrconnect($redirect_uri, $scope, $state);
            echo "<script>window.location.href='" . $url . "'</script>";
            break;
    }
    <?php
    /**
     * Created by PhpStorm.
     * User: 25754
     * Date: 2019/6/4
     * Time: 9:18
     */
    
    define('APPID', "wx1851214902ef11bb");
    define('APPSECRET', "");
    
    class WXOpen
    {
        var $appid = APPID;
        var $appsecret = APPSECRET;
    
        //构造函数,获取Access Token
        public function __construct($appid = NULL, $appsecret = NULL)
        {
            if ($appid && $appsecret) {
                $this->appid = $appid;
                $this->appsecret = $appsecret;
            }
        }
    
        //生成扫码登录的URL
        public function qrconnect($redirect_url, $scope, $state = NULL)
        {
            $url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $this->appid . "&redirect_uri=" . urlencode($redirect_url) . "&response_type=code&scope=" . $scope . "&state=" . $state . "#wechat_redirect";
            return $url;
        }
    
        //生成OAuth2的Access Token
        public function oauth2_access_token($code)
        {
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $code . "&grant_type=authorization_code";
            $res = $this->http_request($url);
            return json_decode($res, true);
        }
    
        //获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取)
        public function oauth2_get_user_info($access_token, $openid)
        {
            $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
            $res = $this->http_request($url);
            return json_decode($res, true);
        }
    
    
        //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
        protected function http_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);
            if (!empty($data)) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }
    }

    结果如图:

  • 相关阅读:
    c# influxDB
    ASP.NET Web简单开发
    vue3 最长递增子序列 diff优化
    【转】Android Kotlin协程 coroutines 理解
    基于混合模型的语音降噪实践
    语音降噪论文“A Hybrid Approach for Speech Enhancement Using MoG Model and Neural Network Phoneme Classifier”的研读
    基于sinc的音频重采样(二):实现
    基于sinc的音频重采样(一):原理
    深度学习中神经网络模型的量化
    嵌入式设备上卷积神经网络推理时memory的优化
  • 原文地址:https://www.cnblogs.com/yang-2018/p/10972765.html
Copyright © 2011-2022 走看看