1. 程式人生 > 程式設計 >php微信授權登入例項講解

php微信授權登入例項講解

要使用微信授權登入功能需要先在微信開發平臺建立應用。然後會獲取微信提供給你的appIdAppSecret,然後就可以進行開發了。
當然現有很多大佬封裝的微信類庫非常齊全,而且還很好用,可以去試試,下面講解一下基本實現方法。

流程

  • 使用者同意授權後獲取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獲取就可以。下面是一個簡單的移動端獲取使用者資訊的方法,使用第二步和第四步就可以了。

publicfunction get_user_info($code){
// 通過code獲取access_token
    $get_token_url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=". $this->appid ."&secret=". $this->appsecret ."&code={$code}&grant_type=authorization_code";
    $token_info = $this->https_request($get_token_url);
    $token_info = 
js
on_decode($token_info,true); if(isset($token_info['errcode'])){ $this->errCode = $token_info['errcode']; $this->errMsg = $token_info['errmsg']; returnfalse; } // 通過access_token和openid獲取使用者資訊 $get_userinfo_url ='https://api.weixin.qq.com/sns/userinfo?access_token='. $token_info['access_token'].'&openid='. $token_info['openid'].'&lang=zh_CN'; $userinfo = $this->https_request($get_userinfo_url); $userinfo = json_decode($userinfo,true); if(isset($userinfo['errcode'])){ $this->errCode = $userinfo['errcode']; $this->errMsg = $userinfo['errmsg']; returnfalse; } return $userinfo; }

封裝成公共類如下

<?php
/**
* 微信授權登入獲取使用者資訊
* @param $appid 微信應用appid
* @param $appsecret 微信應用appsecret
* @author codehui <[email protected]> 2018-3-26
*/
classWxOauth
{
private $appid ="";// appid
private $appsecret ="";// appsecret
publi程式設計客棧c $error =[];// 錯誤資訊
const GET_ACCESS_TOKEN_UR程式設計客棧L ='https://api.weixin.qq.com/sns/oauth2/access_token';// 獲取access_token url
const GET_USER_INFO_URL ='https://api.weixin.qq.com/sns/userinfo';// 獲取使用者資訊url
const GET_REFRESH_URL ='https://api.weixin.qq.com/sns/oauth2/refresh_token';//重新整理access_token
const GET_CODE ='https://open.weixin.qq.com/connect/oauth2/authorize';// 獲取code(網頁授權使用)
publicfunction __construct($appid,$appsecret){
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
}
/**
* 微信登入
* @param string $code 客戶端傳回的code(網頁授權時呼叫getCode方法獲取code,微信會把code返回給redirect_uri)
* @return array 使用者資訊
* @example 錯誤時微信會返回錯誤碼等資訊 eg:{"errcode":,"errmsg":""}
*/
publicfunction wxLogin($code){
$token_info = $this->getToken($code);
if(isset($token_info['errcode'])){
$this->error = $token_info;
returnfalse;
}
$user_info = $this->getUserinfo($token_info['openid'],$token_info['access_token']);
if(isset($user_info['errcode'])){
$this->error = $user_info;
returnfalse;
}
return $user_info;
}
/**
* 使用者同意授權獲取code
* @param string $redirect_uri 授權後重定向的回撥連結地址,需要urlEncode處理
* @return redirect
*/
publicfunction getCode($redirect_uri){
$uri = $this->combineURL(self::GET_CODE,[
'appid'=> $this->appid,'scope'=>'SCOPE','response_type'=>'code','redirect_uri'=> urlEncode($redirect_uri),'state'=>'STATE#wechat_redirect',]);
header('Location: '. $uri,true);
}
/**
* 獲取token和openid
* @param string $code 客戶端傳回的code
* @return array 獲取到的資料
*/
publicfunction getToken($code){
$get_token_url = $this->combineURL(self::GET_ACCESS_TOKEN_URL,[
'appid'=>程式設計客棧; $this->appid,'appsecret'=> $this->appsecret,'code'=> $code,'grant_type'=>'authorization_code'
]);
$token_info = $this->httpsRequest($get_token_url);
return json_decode($token_info,true);
}
/**
* 重新整理access token並續期
* @param string $refresh_token 使用者重新整理access_token
* @return array
*/
publicfunction refreshToken($refresh_token){
$refresh_token_url = $this->combineURL(self::GET_REFRESH_URL,'refresh_token'=> $refresh_token,'grant_type'=>'refresh_token'
]);
$refresh_info = $this->httpsRequest($refresh_token_url);
return json_decode($refresh_info,true);
}
/**
* 獲取使用者資訊
* @param string $openid 使用者的標識
* @param string $access_token 呼叫介面憑證
* @return array 使用者資訊
*/
publicfunction getUserinfo($openid,$access_token){
$get_userinfo_url = $this->combineURL(self::GET_USER_INFO_URL,[
'openid'=> $openid,'access_token'=> $access_token,'lang'=>'zh_CN'
]);
$user_info = $this->httpsRequest($get_userinfo_url);
return json_decode($user_info,true);
}
/**
* 拼接url
* @param string $baseURL 請求的url
* @param arr程式設計客棧ay $keysArr 引數列表陣列
* @return string 返回拼接的url
*/
publicfunction combineURL($baseURL,$keysArr){
$combined = $baseURL ."?";
$valueArr = array();
foreach($keysArr as $key => $val){
$valueArr[]="$key=$val";
}
$keyStr = implode("&",$valueArr);
$combined .=($keyStr);
return $combined;
}
/**
* 獲取伺服器資料
* @param string $url 請求的url
* @return unknown 請求返回的內容
*/
publicfunction httpsRequest($url){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($curl);
curl_close($curl);
return $outputcNpffWV;
}
}

使用方法

// 移動端使用
$WxOauth =newWxOauth(APPID,APPSECRET);// 傳入appid和appsecret
//公眾號登入需要先獲取code,下面方法會自動跳轉到微信授權頁面
$WxOauth->getCode();
// 通過移動端傳來的code或者微信回撥返回的code獲取使用者資訊
$user_info = $WxOauth->wxLogin($_REQUEST['code']);
if($user_info){
//獲取到使用者資訊
}else{
// 獲取錯誤
$WxOauth->error;
}

到此這篇關於php微信授權登入例項講解的文章就介紹到這了,更多相關php微信授權登入內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!