濮阳杆衣贸易有限公司

主頁 > 知識庫 > php微信授權(quán)登錄實(shí)例講解

php微信授權(quán)登錄實(shí)例講解

熱門標(biāo)簽:衡水外呼系統(tǒng)平臺 新河科技智能外呼系統(tǒng)怎么樣 地圖標(biāo)注平臺怎么給錢注冊 注冊400電話申請 安裝電銷外呼系統(tǒng) 福州人工外呼系統(tǒng)哪家強(qiáng) 百度商鋪地圖標(biāo)注 常州地圖標(biāo)注服務(wù)商 釘釘打卡地圖標(biāo)注

要使用微信授權(quán)登錄功能需要先在微信開發(fā)平臺創(chuàng)建應(yīng)用。然后會獲取微信提供給你的appIdAppSecret,然后就可以進(jìn)行開發(fā)了。
當(dāng)然現(xiàn)有很多大佬封裝的微信類庫非常齊全,而且還很好用,可以去試試,下面講解一下基本實(shí)現(xiàn)方法。

流程

  • 用戶同意授權(quán)后獲取code,code有效期10分鐘
  • 使用code獲取access_token調(diào)用接口憑證,有效期2小時
  • refresh_token當(dāng)access_token過期可以使用這個進(jìn)行刷新,有效期30天
  • openid普通用戶的標(biāo)識
  • 刷新token
  • 通過token和openid獲取用戶信息

若access_token已超時,那么進(jìn)行refresh_token會獲取一個新的access_token,新的超時時間。若access_token未超時,那么進(jìn)行refresh_token不會改變access_token,但超時時間會刷新,相當(dāng)于續(xù)期access_token。
refresh_token擁有較長的有效期(30天),當(dāng)refresh_token失效的后,需要用戶重新授權(quán)。

獲取用戶信息

移動端開發(fā)由移動端獲取code,網(wǎng)頁開發(fā)用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 = json_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
/**
* 微信授權(quán)登錄獲取用戶信息
* @param $appid 微信應(yīng)用appid
* @param $appsecret 微信應(yīng)用appsecret
* @author codehui admin@codehui.net> 2018-3-26
*/
classWxOauth
{
private $appid ="";// appid
private $appsecret ="";// appsecret
public $error =[];// 錯誤信息
const GET_ACCESS_TOKEN_URL ='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(網(wǎng)頁授權(quán)使用)
publicfunction __construct($appid, $appsecret){
if($appid  $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
}
/**
* 微信登錄
* @param string $code 客戶端傳回的code(網(wǎng)頁授權(quán)時調(diào)用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;
}
/**
* 用戶同意授權(quán)獲取code
* @param string $redirect_uri 授權(quán)后重定向的回調(diào)鏈接地址,需要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 獲取到的數(shù)據(jù)
*/
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并續(xù)期
* @param string $refresh_token 用戶刷新access_token
* @return array
*/
publicfunction refreshToken($refresh_token){
$refresh_token_url = $this->combineURL(self::GET_REFRESH_URL,[
'appid'=> $this->appid,
'refresh_token'=> $refresh_token,
'grant_type'=>'refresh_token'
]);
$refresh_info = $this->httpsRequest($refresh_token_url);
return json_decode($refresh_info,true);
}
/**
* 獲取用戶信息
* @param string $openid 用戶的標(biāo)識
* @param string $access_token 調(diào)用接口憑證
* @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 array $keysArr 參數(shù)列表數(shù)組
* @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;
}
/**
* 獲取服務(wù)器數(shù)據(jù)
* @param string $url 請求的url
* @return unknown 請求返回的內(nèi)容
*/
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, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}

使用方法

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

到此這篇關(guān)于php微信授權(quán)登錄實(shí)例講解的文章就介紹到這了,更多相關(guān)php微信授權(quán)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • thinkphp5框架結(jié)合mysql實(shí)現(xiàn)微信登錄和自定義分享鏈接與圖文功能示例
  • PHP實(shí)現(xiàn)網(wǎng)站應(yīng)用微信登錄功能詳解
  • php的laravel框架快速集成微信登錄的方法

標(biāo)簽:六安 鷹潭 白城 遼陽 唐山 克拉瑪依 柳州 鶴崗

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php微信授權(quán)登錄實(shí)例講解》,本文關(guān)鍵詞  php,微信,授權(quán),登錄,實(shí)例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php微信授權(quán)登錄實(shí)例講解》相關(guān)的同類信息!
  • 本頁收集關(guān)于php微信授權(quán)登錄實(shí)例講解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    上饶县| 泸州市| 南阳市| 深泽县| 花莲市| 南丰县| 朝阳县| 纳雍县| 北安市| 三河市| 怀宁县| 甘肃省| 台州市| 正镶白旗| 怀化市| 西和县| 廊坊市| 富裕县| 延寿县| 新乡市| 潞西市| 崇礼县| 淄博市| 台江县| 明星| 布尔津县| 永仁县| 汉阴县| 札达县| 察哈| 松阳县| 永胜县| 南阳市| 新昌县| 高雄市| 昆山市| 汕头市| 拜城县| 高雄县| 门头沟区| 故城县|