php微信授權(quán)登錄實(shí)例講解
要使用微信授權(quán)登錄功能需要先在微信開發(fā)平臺(tái)創(chuàng)建應(yīng)用。然后會(huì)獲取微信提供給你的appId
和AppSecret
,然后就可以進(jìn)行開發(fā)了。
當(dāng)然現(xiàn)有很多大佬封裝的微信類庫非常齊全,而且還很好用,可以去試試,下面講解一下基本實(shí)現(xiàn)方法。
流程
- 用戶同意授權(quán)后獲取
code
,code有效期10分鐘 - 使用code獲取
access_token
調(diào)用接口憑證,有效期2小時(shí) refresh_token
當(dāng)access_token過期可以使用這個(gè)進(jìn)行刷新,有效期30天- openid普通用戶的標(biāo)識(shí)
- 刷新
token
- 通過token和openid獲取用戶信息
若access_token已超時(shí),那么進(jìn)行refresh_token會(huì)獲取一個(gè)新的access_token,新的超時(shí)時(shí)間。若access_token未超時(shí),那么進(jìn)行refresh_token不會(huì)改變access_token,但超時(shí)時(shí)間會(huì)刷新,相當(dāng)于續(xù)期access_token。
refresh_token擁有較長的有效期(30天),當(dāng)refresh_token失效的后,需要用戶重新授權(quán)。
獲取用戶信息
移動(dòng)端開發(fā)由移動(dòng)端獲取code,網(wǎng)頁開發(fā)用php獲取就可以。下面是一個(gè)簡單的移動(dòng)端獲取用戶信息的方法,使用第二步和第四步就可以了。
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 =[];// 錯(cuò)誤信息 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)時(shí)調(diào)用getCode方法獲取code,微信會(huì)把code返回給redirect_uri) * @return array 用戶信息 * @example 錯(cuò)誤時(shí)微信會(huì)返回錯(cuò)誤碼等信息 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)識(shí) * @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 請(qǐng)求的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 請(qǐng)求的url * @return unknown 請(qǐng)求返回的內(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; } }
使用方法
// 移動(dòng)端使用 $WxOauth =newWxOauth(APPID, APPSECRET);// 傳入appid和appsecret //公眾號(hào)登錄需要先獲取code,下面方法會(huì)自動(dòng)跳轉(zhuǎn)到微信授權(quán)頁面 $WxOauth->getCode(); // 通過移動(dòng)端傳來的code或者微信回調(diào)返回的code獲取用戶信息 $user_info = $WxOauth->wxLogin($_REQUEST['code']); if($user_info){ //獲取到用戶信息 }else{ // 獲取錯(cuò)誤 $WxOauth->error; }
到此這篇關(guān)于php微信授權(quán)登錄實(shí)例講解的文章就介紹到這了,更多相關(guān)php微信授權(quán)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP之浮點(diǎn)數(shù)計(jì)算比較以及取整數(shù)不準(zhǔn)確的解決辦法
這篇文章主要介紹了PHP之浮點(diǎn)數(shù)計(jì)算比較以及取整數(shù)不準(zhǔn)確的解決辦法,代碼超簡單,需要的朋友可以參考下2015-07-07Zend Framework教程之路由功能Zend_Controller_Router詳解
這篇文章主要介紹了Zend Framework教程之路由功能Zend_Controller_Router,詳細(xì)分析了路由功能Zend_Controller_Router的原理,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03ThinkPHP模板中數(shù)組循環(huán)實(shí)例
這篇文章主要介紹了ThinkPHP模板中數(shù)組循環(huán),以實(shí)例形式展示了ThinkPHP采用foreach標(biāo)簽循環(huán)輸出數(shù)組的方法,需要的朋友可以參考下2014-10-10laravel實(shí)現(xiàn)圖片上傳預(yù)覽,及編輯時(shí)可更換圖片,并實(shí)時(shí)變化的例子
今天小編就為大家分享一篇laravel實(shí)現(xiàn)圖片上傳預(yù)覽,及編輯時(shí)可更換圖片,并實(shí)時(shí)變化的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11