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); 
    }