在公眾號的配置過程中,許多開發者會在菜單中加入HTML5頁面,有時在頁面內需要訪問頁面的用戶信息,此時就需要網頁授權獲取用戶基本信息
PS:本博文所闡述的微信開發基于Yii2.0框架
1、設置授權回調域名:開發 ---> 接口權限
找到“網頁授權獲取用戶基本信息”,點擊后面對應的“修改”,在彈框響應位置填寫授權回調域名即可,此處的域名不需要加http:// (關于網頁授權回調域名的說明詳情可參考公眾平臺開發者文檔)


2、獲取授權
關于OAuth2.0博主參考的是方倍工作室的博文http://m.survivalescaperooms.com/txw1958/p/weixin71-oauth20.html(PS:方倍是一個微信開發大神,其中的微信開發內容還是比較詳細的,推薦參考),其中詳細剖析了微信官方文檔的相關內容,也提供了獲取授權的更詳細思路和方案。
實際上,獲取用戶信息的關鍵在于獲取用戶的openid。博主想要實現用戶點擊公眾號菜單打開頁面即可自動授權,從而針對該用戶進行數據庫操作,于是有下面兩種方式:
(1)利用自定義菜單請求授權頁面
自定義菜單后面會單獨寫一篇博文,在這里先簡述一下通過自定義菜單進行授權,該方法需要高級接口權限,且局限于關注公眾號的用戶直接從菜單進入頁面。
1 $menu = '{ 2 "button":[ 3 { 4 "type": "view", 5 "name": "商城", 6 "url": "https://open.weixin.QQ.com/connect/oauth2/authorize?appid=xxx&redirect_uri=http://tx.heivr.com/index.php&response_type=code&scope=snsapi_base&state=1#wechat_redirect" 7 }, 8 9 {10 "name":"快遞服務",11 "sub_button":[12 {13 "type":"click",14 "name":"發快遞",15 "key":"ex16 },17 {18 "type":"click",19 "name":"快遞查詢",20 "key":"ww"21 }22 ]23 },24 ]25 }';
需要授權的view直接在url處填寫微信提供的授權請求地址,其中:
1、以snsapi_base為scope發起的網頁授權,是用來獲取進入頁面的用戶的openid的,并且是靜默授權并自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業務頁面)2、以snsapi_userinfo為scope發起的網頁授權,是用來獲取用戶的基本信息的。但這種授權需要用戶手動同意,并且由于用戶同意過,所以無須關注,就可在授權后獲取該用戶的基本信息。按照此方法點擊“商城”即可接收到返回的openid,繼而進行下一步用戶信息的獲取。
(2)利用JS自動請求授權頁面
這個方法相對而言比較笨拙,步驟略復雜,但目前能解決需求還沒有研究簡化方法,且由于頁面的跳轉多數情況下訪問頁面的時間會增加,但相比于前一個方法,該方法可以獲取到非關注用戶的基本信息。有些程序可能涉及到頁面分享,程序沒有強制關注但其他用戶通過分享直接進入頁面也需要記錄用戶信息,此時可以考慮該方法。(微信開發相關的代碼博主封裝成工具類調用,這里先貼用到的部分,以后整理完成會全部貼出來并附下載鏈接)
該方法的思路為:js請求鏈接獲取code ---> 利用code換取openid ---> 得到用戶基本信息
a. 編輯配置
為了方便把用到的一些微信參數單獨寫入一個類,方便修改添加及調用
1 <?php 2 namespace common/tools/wechat; 3 4 /** 5 * 微信請求相關配置類庫 6 */ 7 class ConfigTool { 8 9 /**10 * 微信配置參數11 * @return array 配置參數12 */13 public function setConfig() {14 15 // 用于驗證微信接口配置信息的Token,可以任意填寫16 $config['token'] = '自己的token';17 18 // appID19 $config['appid'] = '自己的appid';20 21 // appSecret22 $config['secret'] = '自己的secret';23 24 // 回調鏈接地址25 $config['redirect_uri'] = 'http://tx.heivr.com/index.php?';26 27 // 是否以 HTTPS 安全協議訪問接口28 $config['https_request'] = false;29 30 // 授權作用域,snsapi_base (不彈出授權頁面,直接跳轉,只能獲取用戶openid),31 // snsapi_userinfo (彈出授權頁面,可通過openid拿到昵稱、性別、所在地。并且,32 // 即使在未關注的情況下,只要用戶授權,也能獲取其信息)33 $config['scope'] = 'snsapi_userinfo';34 35 // 語言36 $config['lang'] = 'zh_CN'; // zh_CN 簡體,zh_TW 繁體,en 英語37 38 // 微信公眾賬戶授權地址39 $config['mp_authorize_url'] = 'https://api.weixin.qq.com/cgi-bin/token';40 // 微信公眾賬戶js臨時票據地址41 $config['jsapi_ticket_url'] = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';42 // 授權地址43 $config['authorize_url'] = 'https://open.weixin.qq.com/connect/oauth2/authorize';44 // 獲取access token 的地址45 $config['access_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/access_token';46 // 刷新 token 的地址47 $config['refresh_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';48 // 獲取用戶信息地址49 $config['userinfo_url'] = 'https://api.weixin.qq.com/sns/userinfo';50 // 驗證access token51 $config['valid_token_url'] = 'https://api.weixin.qq.com/sns/auth';52 // 上傳臨時素材地址53 $config['media_temp_upload_url'] = 'https://api.weixin.qq.com/cgi-bin/media/upload?';54 // 上傳永久素材地址55 $config['media_forever_upload_url'] = 'https://api.weixin.qq.com/cgi-bin/material/add_material?';56 57 return $config;58 59 }60 }
b. https請求工具
1 <?php 2 namespace common/tools; 3 4 /** 5 * https請求相關類庫 6 */ 7 class HttpsTool { 8 9 const TIMEOUT = 5; // 設置超時時間10 11 private $ch; // curl對象12 13 /**14 * 發送curl請求,并獲取請求結果15 * @param string 請求地址16 * @param array 如果是post請求則需要傳入請求參數17 * @param string 請求方法,get 或者 post, 默認為get18 * @param bool 是否以https協議請求19 */20 public function send_request($requests, $params = null, $method = 'get', $https = true) {21 // 以get方式提交22 if ($method == 'get') {23 if($params){24 $request = $requests . $this->create_url($params);25 }else{26 $request = $requests;27 }28 }else{29 $request = $requests;30 }31 32 $this->ch = curl_init($request);33 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);// 設置不顯示結果,儲存入變量34 curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT); // 設置超時限制防止死循環35 36 // 判斷是否以https方式訪問37 if ($https) {38 curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查39 curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0); // 從證書中檢查SSL加密算法是否存在40 }41 42 if ($method == 'post') { // 以post方式提交43 //curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false); //php 5.6文件上傳必加內容,5.4不需要44 curl_setopt($this->ch, CURLOPT_POST, 1); // 發送一個常規的Post請求45 curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params); // Post提交的數據包46 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);47 }48 49 $tmpInfo = curl_exec($this->ch); // 執行操作50 if (curl_errno($this->ch)) {51 echo 'Errno:'.curl_error($this->ch);//捕抓異常52 }53 curl_close($this->ch); // 關閉CURL會話54 //var_dump($tmpInfo);exit;55 return $tmpInfo; // 返回數據56 }57 58 /**59 * 生成url60 */61 public function create_url($data) {62 $temp = '?';63 foreach ($data as $key => $item) {64 $temp = $temp . $key . '=' . $item . '&';65 }66 return substr($temp, 0, -1);67 }68 }
關于curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false)會在微信圖片資源上傳博文中詳細講述它出現的心酸史,這里暫時用不到,不做解釋
c. 授權基類
1 <?php 2 namespace common/tools/wechat; 3 4 use common/tools/wechat/ConfigTool; 5 use common/tools/HttpsTool; 6 /** 7 * Weixin_oauth 類庫 8 */ 9 class OauthTool {10 11 public $conf;12 13 public function __construct(){14 $re = new ConfigTool; 15 $this->conf = $re->setConfig();16 } 17 18 /**19 * 生成用戶授權的地址20 * @param string 自定義需要保持的信息21 * @param sting 請求的路由22 * @param bool 是否是通過公眾平臺方式認真23 */24 public function authorize_addr($route, $state='', $mp=false) {25 26 if ($mp) {27 $data = [28 'appid' => $this->conf['appid'],29 'secret' => $this->conf['token'],30 'grant_type' => 'client_credential'31 ];32 $url = $this->conf['mp_authorize_url'];33 } else {34 $data = [35 'appid' => $this->conf['appid'], //公眾號唯一標識36 'redirect_uri' => urlencode($this->conf['redirect_uri'] . $route), //授權后重定向的回調鏈接地址37 'response_type' => 'code', //返回類型,此處填寫code38 'scope'=>$this->conf['scope'], //應用授權作用域39 'state'=>$state, //重定向后帶上state參數,開發者可以填寫任意參數40 '#wechat_redirect'=>'' //直接在微信打開鏈接,可不填,做頁面302重定向時必須帶此參數41 ];42 $url = $this->conf['authorize_url'];43 }44 45 $send = new HttpsTool;46 //var_dump($url . $send->create_url($data));exit;47 return $url . $send->create_url($data);48 }49 50 /**51 * 獲取 access token52 * @param string 用于換取access token的code,微信提供53 */54 public function access_token($code) {55 56 $data = [57 'appid' => $this->conf['appid'],58 'secret' => $this->conf['secret'],59 'code' => $code,60 'grant_type' => 'authorization_code'61 ];62 // 生成授權url63 $url = $this->conf['access_token_url'];64 65 $send = new HttpsTool;66 return $send->send_request($url, $data);67 }68 69 /**70 * 獲取用戶信息71 * @param string access token72 * @param string 用戶的open id73 */74 public function userinfo($token, $openid) {75 76 $data = [77 'access_token' => $token,78 'openid' => $openid,79 'lang' => $this->conf['lang']80 ];81 // 生成授權url82 $url = $this->conf['userinfo_url'];83 84 $send = new HttpsTool;85 return $send->send_request($url, $data);86 }87 88 }
d. 授權基類調用及用戶數據處理(在控制器調用前,先對用戶數據存入或更新)
1 <?php 2 namespace wechat/controllers/classes; 3 4 use common/tools/wechat/OauthTool; 5 use common/models/User; 6 use common/tools/EmojiTool; 7 8 /** 9 * 微信用戶基本信息獲取10 */11 class UserinfoClass {12 13 /**14 * 用戶授權并獲取code 15 * @return string 用戶code16 */17 public function getCode($route, $state){18 19 $re = new OauthTool;20 $request = $re->authorize_addr($route, $state);21 $code = isset($_GET['code']) ? $_GET['code'] : '';22 23 return [$request,$code];24 }25 26 /**27 * 獲取用戶信息并寫入數據庫(之后加參數傳給code)28 */29 public function info($code) {30 $re = new OauthTool;31 //獲取access token32 $access = $re->access_token($code);33 $token = json_decode($access,true);34 //header("Content-type: text/html; charset=gbk"); 35 //獲取用戶信息36 if(count($token) != 2) {37 $response = $re->userinfo($token['access_token'], $token['openid']);38 $user = json_decode($response,true);39 //用戶昵稱轉換40 //$user['nickname'] = EmojiTool::emoji_trans($user['nickname']);41 42 if($model = User::findOne(['openid' => $user['openid'] ])) { //用戶已存在更新數據43 $model->attributes = $user;44 $model->modify_time = time();45 $model->save(false);46 }else{ //用戶不存在寫入47 $model = new User;48 $model->attributes = $user;49 $model->create_time = time();50 $model->save(false);51 }52 }53 return isset($model->id) ? $model->id : '';54 }55 56 }
e. 控制器調用(這里只貼其中一個方法)
1 /** 2 * 產品列表 3 * @return object 所有可用產品信息 4 */ 5 public function actionIndex(){ 6 //判斷頁面是否自動刷新 7 if(isset($_GET['state'])) { 8 $refresh = 0; 9 }else{10 $refresh = 1;11 }12 13 //獲取用戶code14 $user = new UserinfoClass;15 $request = $user->getCode('r=store/index', 1);16 17 //該用戶userid18 $userid = $user->info($request[1]);19 20 $model = new Product;21 $list = $model->find()->where(['status' => 1])->all();22 23 return $this->render('index',['list' => $list, 'refresh' => $refresh, 'userid' => $userid, 'request' => $request]);24 }
程序要求用戶打開產品列表即獲取用戶信息并存入數據庫,其中設計了幾個變量作用如下:
$refresh:判斷頁面是否刷新,由于首次打開頁面未進行oauth驗證時才自動請求驗證,避免反復刷新,這里用回調的state參數作為判斷依據且設state=1(若有特定參數需要可將state賦值為所需值);
$request:即為驗證請求地址
f. 視圖自動刷新
只需要在視圖中添加以下js代碼即可
1 <script type="text/javascript"> 2 3 //自動請求獲取code 4 $(function(){ 5 var refresh = <?= $refresh; ?>; 6 var request = '<?= $request[0]; ?>'; 7 if(refresh == 1){ 8 console.log(1); 9 location = request;10 }11 });12 </script>
特此聲明:相關文章均為查閱資料、閱讀大神博文后結合實際開發情況遇到的問題整理而成,能找到原博的必會署名,找不到原博而引用的內容還望原博主海涵
新聞熱點
疑難解答