要使用微信授權登錄功能需要先在微信開發平台創建應用。然后會獲取微信提供給你的appId
和AppSecret
,然后就可以進行開發了。
當然現有很多大佬封裝的微信類庫非常齊全,而且還很好用,可以去試試,下面講解一下基本實現方法。
流程
- 用戶同意授權后獲取code,code有效期10分鍾
- 使用code獲取
access_token
調用接口憑證,有效期2小時refresh_token
當access_token過期可以使用這個進行刷新,有效期30天openid
普通用戶的標識 - 刷新token
- 通過token和openid獲取用戶信息
若access_token已超時,那么進行refresh_token會獲取一個新的access_token,新的超時時間。若access_token未超時,那么進行refresh_token不會改變access_token,但超時時間會刷新,相當於續期access_token。
refresh_token擁有較長的有效期(30天),當refresh_token失效的后,需要用戶重新授權。
獲取用戶信息
移動端開發由移動端獲取code
,網頁開發用php獲取就可以。下面是一個簡單的移動端獲取用戶信息的方法,使用第二步和第四步就可以了。
1 publicfunction get_user_info($code){ 2 // 通過code獲取access_token 3 $get_token_url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=". $this->appid ."&secret=". $this->appsecret ."&code={$code}&grant_type=authorization_code"; 4 $token_info = $this->https_request($get_token_url); 5 $token_info = json_decode($token_info,true); 6 if(isset($token_info['errcode'])){ 7 $this->errCode = $token_info['errcode']; 8 $this->errMsg = $token_info['errmsg']; 9 returnfalse; 10 } 11 // 通過access_token和openid獲取用戶信息 12 $get_userinfo_url ='https://api.weixin.qq.com/sns/userinfo?access_token='. $token_info['access_token'].'&openid='. $token_info['openid'].'&lang=zh_CN'; 13 $userinfo = $this->https_request($get_userinfo_url); 14 $userinfo = json_decode($userinfo,true); 15 if(isset($userinfo['errcode'])){ 16 $this->errCode = $userinfo['errcode']; 17 $this->errMsg = $userinfo['errmsg']; 18 returnfalse; 19 } 20 return $userinfo; 21 }
- 封裝成公共類如下,(使用方法)
1 <?php 2 /** 3 * 微信授權登錄獲取用戶信息 4 * @param $appid 微信應用appid 5 * @param $appsecret 微信應用appsecret 6 * @author codehui <admin@codehui.net> 2018-3-26 7 */ 8 classWxOauth 9 { 10 private $appid ="";// appid 11 private $appsecret ="";// appsecret 12 public $error =[];// 錯誤信息 13 const GET_ACCESS_TOKEN_URL ='https://api.weixin.qq.com/sns/oauth2/access_token';// 獲取access_token url 14 const GET_USER_INFO_URL ='https://api.weixin.qq.com/sns/userinfo';// 獲取用戶信息url 15 const GET_REFRESH_URL ='https://api.weixin.qq.com/sns/oauth2/refresh_token';//刷新access_token 16 const GET_CODE ='https://open.weixin.qq.com/connect/oauth2/authorize';// 獲取code(網頁授權使用) 17 publicfunction __construct($appid, $appsecret){ 18 if($appid && $appsecret){ 19 $this->appid = $appid; 20 $this->appsecret = $appsecret; 21 } 22 } 23 /** 24 * 微信登錄 25 * @param string $code 客戶端傳回的code(網頁授權時調用getCode方法獲取code,微信會把code返回給redirect_uri) 26 * @return array 用戶信息 27 * @example 錯誤時微信會返回錯誤碼等信息 eg:{"errcode":, "errmsg":""} 28 */ 29 publicfunction wxLogin($code){ 30 $token_info = $this->getToken($code); 31 if(isset($token_info['errcode'])){ 32 $this->error = $token_info; 33 returnfalse; 34 } 35 $user_info = $this->getUserinfo($token_info['openid'], $token_info['access_token']); 36 if(isset($user_info['errcode'])){ 37 $this->error = $user_info; 38 returnfalse; 39 } 40 return $user_info; 41 } 42 /** 43 * 用戶同意授權獲取code 44 * @param string $redirect_uri 授權后重定向的回調鏈接地址,需要urlEncode處理 45 * @return redirect 46 */ 47 publicfunction getCode($redirect_uri){ 48 $uri = $this->combineURL(self::GET_CODE,[ 49 'appid'=> $this->appid, 50 'scope'=>'SCOPE', 51 'response_type'=>'code', 52 'redirect_uri'=> urlEncode($redirect_uri), 53 'state'=>'STATE#wechat_redirect', 54 ]); 55 header('Location: '. $uri,true); 56 } 57 /** 58 * 獲取token和openid 59 * @param string $code 客戶端傳回的code 60 * @return array 獲取到的數據 61 */ 62 publicfunction getToken($code){ 63 $get_token_url = $this->combineURL(self::GET_ACCESS_TOKEN_URL,[ 64 'appid'=> $this->appid, 65 'appsecret'=> $this->appsecret, 66 'code'=> $code, 67 'grant_type'=>'authorization_code' 68 ]); 69 $token_info = $this->httpsRequest($get_token_url); 70 return json_decode($token_info,true); 71 } 72 /** 73 * 刷新access token並續期 74 * @param string $refresh_token 用戶刷新access_token 75 * @return array 76 */ 77 publicfunction refreshToken($refresh_token){ 78 $refresh_token_url = $this->combineURL(self::GET_REFRESH_URL,[ 79 'appid'=> $this->appid, 80 'refresh_token'=> $refresh_token, 81 'grant_type'=>'refresh_token' 82 ]); 83 $refresh_info = $this->httpsRequest($refresh_token_url); 84 return json_decode($refresh_info,true); 85 } 86 /** 87 * 獲取用戶信息 88 * @param string $openid 用戶的標識 89 * @param string $access_token 調用接口憑證 90 * @return array 用戶信息 91 */ 92 publicfunction getUserinfo($openid, $access_token){ 93 $get_userinfo_url = $this->combineURL(self::GET_USER_INFO_URL,[ 94 'openid'=> $openid, 95 'access_token'=> $access_token, 96 'lang'=>'zh_CN' 97 ]); 98 $user_info = $this->httpsRequest($get_userinfo_url); 99 return json_decode($user_info,true); 100 } 101 /** 102 * 拼接url 103 * @param string $baseURL 請求的url 104 * @param array $keysArr 參數列表數組 105 * @return string 返回拼接的url 106 */ 107 publicfunction combineURL($baseURL, $keysArr){ 108 $combined = $baseURL ."?"; 109 $valueArr = array(); 110 foreach($keysArr as $key => $val){ 111 $valueArr[]="$key=$val"; 112 } 113 $keyStr = implode("&", $valueArr); 114 $combined .=($keyStr); 115 return $combined; 116 } 117 /** 118 * 獲取服務器數據 119 * @param string $url 請求的url 120 * @return unknown 請求返回的內容 121 */ 122 publicfunction httpsRequest($url){ 123 $curl = curl_init(); 124 curl_setopt($curl, CURLOPT_URL, $url); 125 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 126 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 127 curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); 128 $output = curl_exec($curl); 129 curl_close($curl); 130 return $output; 131 } 132 }
使用方法
1 // 移動端使用 2 $WxOauth =newWxOauth(APPID, APPSECRET);// 傳入appid和appsecret 3 //公眾號登錄需要先獲取code,下面方法會自動跳轉到微信授權頁面 4 $WxOauth->getCode(); 5 // 通過移動端傳來的code或者微信回調返回的code獲取用戶信息 6 $user_info = $WxOauth->wxLogin($_REQUEST['code']); 7 if($user_info){ 8 //獲取到用戶信息 9 }else{ 10 // 獲取錯誤 11 $WxOauth->error; 12 }