我們經(jīng)常會(huì)遇到關(guān)于掃碼登入的問(wèn)題,比如說(shuō),自媒體時(shí)代,很多公司、單位、個(gè)人都申請(qǐng)了微信公眾號(hào)。我們會(huì)發(fā)現(xiàn)申請(qǐng)好的微信公眾號(hào)在登入的時(shí)候有兩道關(guān)卡,如下:
1、用戶(hù)名、密碼登入
2、掃碼登入
今天跟大家分享關(guān)于用php實(shí)現(xiàn)掃碼登入的程序開(kāi)發(fā)流程,供參考、學(xué)習(xí)。
1、首先到微信開(kāi)放平臺(tái)申請(qǐng)https://open.weixin.qq.com/ 獲取到appid和appsecret,前臺(tái)顯示頁(yè)面如下
<!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: "wxed782be999f86e0e", scope: "snsapi_login", redirect_uri: encodeURIComponent("http://" + window.location.host + "/login.php"), state: Math.ceil(Math.random()*1000), style: "black", href: ""}); </script> </body> </html> 2、PHP處理代碼頁(yè)面 /* require_once('weixin.class.php'); $weixin = new class_weixin(); */ define('APPID', "wx19ba77624e083e08"); define('APPSECRET', "c1a56a5c4247dd44c320c9719c5ceb90"); class class_weixin { var $appid = APPID; var $appsecret = APPSECRET; //構(gòu)造函數(shù),獲取Access Token public function __construct($appid = NULL, $appsecret = NULL) { if($appid && $appsecret){ $this->appid = $appid; $this->appsecret = $appsecret; } //掃碼登錄不需要該Access Token, 語(yǔ)義理解需要 //1. 本地寫(xiě)入 $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 網(wǎng)站應(yīng)用 */ /* 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); } //獲取用戶(hù)基本信息(OAuth2 授權(quán)的 Access Token 獲取 未關(guān)注用戶(hù),Access Token為臨時(shí)獲取) 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); } 南昌雅騰教育,專(zhuān)注IT技術(shù),主要培訓(xùn):php軟件開(kāi)發(fā)、web前端開(kāi)發(fā) 轉(zhuǎn)行、零基礎(chǔ)、包教會(huì) 小班教學(xué)、手把手輔導(dǎo)、理論與企業(yè)真實(shí)項(xiàng)目開(kāi)發(fā)結(jié)合教學(xué) 知識(shí)改變命運(yùn)、技術(shù)成就高薪 學(xué)習(xí)熱線:15079188802(程老師) QQ:1939594233(微信同號(hào)) |