简单的几句PHP代码实现微信开放平台实现登录

版权声明:转载请注明原创地址 https://blog.csdn.net/u013032788/article/details/80916674

刚刚实现的,好多粉丝要求搞个微信扫码登陆功能,由于以前怕麻烦(申请过程繁琐),前几天一个美女再三要求,于是就搞了下,注意是去微信开放平台申请。

演示参考下二当家的(直接登陆):https://www.erdangjiade.com/php/1034.html


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

前台代码如下,简单一句话。

  1.  
       
    1. <!DOCTYPE html> 
    2. <html> 
    3.     <head> 
    4.         <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
    5.     </head> 
    6.     <body> 
    7.         <span id="login_container"></span> 
    8.         <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> 
    9.         <script> 
    10.             var obj = new WxLogin({ 
    11.               id: "login_container", 
    12.               appid: "wxed782be999f82e0e", 
    13.               scope: "snsapi_login", 
    14.               redirect_uri: encodeURIComponent("http://" + window.location.host + "/login.php"), 
    15.               state: Math.ceil(Math.random()*1000), 
    16.               style: "black", 
    17.               href: ""}); 
    18.         </script> 
    19.     </body> 
    20. </html>
    21.  
    22. 服务器代码如下:
     
       
    1. /* 
    2.     require_once('weixin.class.php'); 
    3.     $weixin = new class_weixin(); 
    4. */ 
    5.  
    6. define('APPID',        "wx19ba7724e0383e08"); 
    7. define('APPSECRET',    "c1a56a5d4247dd44c320c9719c5ceb90"); 
    8.  
    9. class class_weixin 
    10. { 
    11.     var $appid = APPID; 
    12.     var $appsecret = APPSECRET; 
    13.  
    14.     //构造函数,获取Access Token 
    15.     public function __construct($appid = NULL, $appsecret = NULL) 
    16.     { 
    17.         if($appid && $appsecret){ 
    18.             $this->appid = $appid; 
    19.             $this->appsecret = $appsecret; 
    20.         } 
    21.  
    22.         //扫码登录不需要该Access Token, 语义理解需要 
    23.         //1. 本地写入  
    24.         $res = file_get_contents('access_token.json'); 
    25.         $result = json_decode($res, true); 
    26.         $this->expires_time = $result["expires_time"]; 
    27.         $this->access_token = $result["access_token"]; 
    28.  
    29.         if (time() > ($this->expires_time + 3600)){ 
    30.             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret; 
    31.             $res = $this->http_request($url); 
    32.             $result = json_decode($res, true); 
    33.             $this->access_token = $result["access_token"]; 
    34.             $this->expires_time = time(); 
    35.             file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}'); 
    36.         } 
    37.     } 
    38.  
    39.     /* 
    40.     *  PART1 网站应用 
    41.     */ 
    42.  
    43.     /* 
    44.     header("Content-type: text/html; charset=utf-8"); 
    45.     require_once('wxopen.class.php'); 
    46.     $weixin = new class_weixin(); 
    47.     if (!isset($_GET["code"])){ 
    48.         $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
    49.         $jumpurl = $weixin->qrconnect($redirect_url, "snsapi_login", "123"); 
    50.         Header("Location: $jumpurl"); 
    51.     }else{ 
    52.         $oauth2_info = $weixin->oauth2_access_token($_GET["code"]); 
    53.         $userinfo = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']); 
    54.         var_dump($userinfo); 
    55.     } 
    56.     */ 
    57.     //生成扫码登录的URL 
    58.     public function qrconnect($redirect_url, $scope, $state = NULL) 
    59.     { 
    60.         $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"; 
    61.         return $url; 
    62.     } 
    63.  
    64.     //生成OAuth2的Access Token 
    65.     public function oauth2_access_token($code) 
    66.     { 
    67.         $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code"; 
    68.         $res = $this->http_request($url); 
    69.         return json_decode($res, true); 
    70.     } 
    71.  
    72.     //获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取) 
    73.     public function oauth2_get_user_info($access_token, $openid) 
    74.     { 
    75.         $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; 
    76.         $res = $this->http_request($url); 
    77.         return json_decode($res, true); 
    78.     

猜你喜欢

转载自blog.csdn.net/u013032788/article/details/80916674