几句代码实现微信开放平台实现网站登录功能

java大神66 发布于 2018/07/04 18:00
阅读 1K+
收藏 1

几句代码实现微信开放平台实现网站登录功能
刚刚实现的,好多粉丝要求搞个微信扫码登陆功能,由于以前怕麻烦(申请过程繁琐),前几天一个美女再三要求,于是就搞了下,注意是去微信开放平台申请。
演示下载参考下这里:http://t.cn/ROuy90K

 

1、首先到微信开放平台申请https://open.weixin.qq.com/ 获取到appid和APPSECRET,前台显示页面如下
前台代码如下,简单一句话。

<!DOCTYPE html> 
<html> 
    <head> 
        <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
    </head> 
    <body> 
        <span id="login_container"></span> 
        <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> 
        <script> 
            var obj = new WxLogin({ 
              id: "login_container", 
              appid: "wxed782be999f82e0e", 
              scope: "snsapi_login", 
              redirect_uri: encodeURIComponent("http://" + window.location.host + "/login.php"), 
              state: Math.ceil(Math.random()*1000), 
              style: "black", 
              href: ""}); 
        </script> 
    </body> 
</html>
 
服务器代码如下:
/* 
    require_once('weixin.class.php'); 
    $weixin = new class_weixin(); 
*/ 
 
define('APPID',        "wx19ba7724e0383e08"); 
define('APPSECRET',    "c1a56a5d4247dd44c320c9719c5ceb90"); 
 
class class_weixin 
{ 
    var $appid = APPID; 
    var $appsecret = APPSECRET; 
 
    //构造函数,获取Access Token 
    public function __construct($appid = NULL, $appsecret = NULL) 
    { 
        if($appid && $appsecret){ 
            $this->appid = $appid; 
            $this->appsecret = $appsecret; 
        } 
 
        //扫码登录不需要该Access Token, 语义理解需要 
        //1. 本地写入  
        $res = file_get_contents('access_token.json'); 
        $result = json_decode($res, true); 
        $this->expires_time = $result["expires_time"]; 
        $this->access_token = $result["access_token"]; 
 
        if (time() > ($this->expires_time + 3600)){ 
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret; 
            $res = $this->http_request($url); 
            $result = json_decode($res, true); 
            $this->access_token = $result["access_token"]; 
            $this->expires_time = time(); 
            file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}'); 
        } 
    } 
 
    /* 
    *  PART1 网站应用 
    */ 
 
    /* 
    header("Content-type: text/html; charset=utf-8"); 
    require_once('wxopen.class.php'); 
    $weixin = new class_weixin(); 
    if (!isset($_GET["code"])){ 
        $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
        $jumpurl = $weixin->qrconnect($redirect_url, "snsapi_login", "123"); 
        Header("Location: $jumpurl"); 
    }else{ 
        $oauth2_info = $weixin->oauth2_access_token($_GET["code"]); 
        $userinfo = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']); 
        var_dump($userinfo); 
    } 
    */ 
    //生成扫码登录的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); 
    }

 

加载中
0
java大神66
java大神66

Java大神6666

0
lyle_luo
lyle_luo

二当家的是PHP

0
2
2442517155

http://www.pansoso.com/zh/%E5%8D%9A%E4%B9%90%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E6%98%8C%E5%90%89%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E9%98%9C%E5%BA%B7%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E5%BA%93%E5%B0%94%E5%8B%92%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E9%98%BF%E5%9B%BE%E4%BB%80%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E4%B9%8C%E8%8B%8F%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E6%8B%89%E8%90%A8%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E6%97%A5%E5%96%80%E5%88%99%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E9%93%B6%E5%B7%9D%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E7%9F%B3%E5%98%B4%E5%B1%B1%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E5%90%B4%E5%BF%A0%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E5%9B%BA%E5%8E%9F%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E4%B8%AD%E5%8D%AB%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E9%9D%92%E9%93%9C%E5%B3%A1%E5%B8%82%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E7%81%B5%E6%AD%A6%E5%B8%82%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E5%91%BC%E5%92%8C%E6%B5%A9%E7%89%B9%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E5%8C%85%E5%A4%B4%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E4%B9%8C%E6%B5%B7%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
http://www.pansoso.com/zh/%E8%B5%A4%E5%B3%B0%E5%8A%9E%E5%B7%A5%E7%A8%8B%E5%B8%88%E8%B5%84%E6%A0%BC%E8%AF%81%E3%80%96%2B%E5%BE%AEv%E4%BF%A1buigfd68%E3%80%97/
 

返回顶部
顶部