微信網(wǎng)頁(yè)授權(quán)(OAuth2.0) PHP 源碼簡(jiǎn)單實(shí)現(xiàn)
提要:
1. 建議對(duì)OAuth2.0協(xié)議做一個(gè)學(xué)習(xí)。
2. 微信官方文檔和微信官網(wǎng)工具要得到充分利用。
比較簡(jiǎn)單,直接帖源代碼了。其中“xxxxxxxxxx”部分,是需要依據(jù)自己環(huán)境做替換的
/** * OAuth2.0微信授權(quán)登錄實(shí)現(xiàn) * * @author zzy * @文件名:GetWxUserInfo.php */ // 回調(diào)地址 $url = urlencode("http://www.xxxxxxxxx.com/GetWxUserInfo.php"); // 公眾號(hào)的id和secret $appid = 'xxxxxxxxx'; $appsecret = 'xxxxxxxxx'; session_start(); // 獲取code碼,用于和微信服務(wù)器申請(qǐng)token。 注:依據(jù)OAuth2.0要求,此處授權(quán)登錄需要用戶端操作 if(!isset($_GET['code']) && !isset($_SESSION['code'])){ echo '<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6c11a252ff1d00c4 &redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"> <font style="font-size:30">授權(quán)</font></a>'; exit; } // 依據(jù)code碼去獲取openid和access_token,自己的后臺(tái)服務(wù)器直接向微信服務(wù)器申請(qǐng)即可 if (isset($_GET['code']) && !isset($_SESSION['token'])){ $_SESSION['code'] = $_GET['code']; $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid. "&secret=".$appsecret."&code=".$_GET['code']."&grant_type=authorization_code"; $res = https_request($url); $res=(json_decode($res, true)); $_SESSION['token'] = $res; } print_r($_SESSION); // 依據(jù)申請(qǐng)到的access_token和openid,申請(qǐng)Userinfo信息。 if (isset($_SESSION['token']['access_token'])){ $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$_SESSION['token']['access_token']."&openid=".$_SESSION['token']['openid']."&lang=zh_CN"; echo $url; $res = https_request($url); $res = json_decode($res, true); $_SESSION['userinfo'] = $res; } print_r($_SESSION); // cURL函數(shù)簡(jiǎn)單封裝 function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
得到正確結(jié)果如下:
Array ( [code] => 041GZI4l0tvGHg10N75l05FQ4l0GZI42 [token] => Array ( [access_token] => TWo6w5QMpzTZibu3FPh2k4EdC5bllp4sGeQkC4NbZtj-zti-ctZj1SrrNL1qGCf2lB1-6o3N7kh2bcxl5bxtQqJEGk1cq12l8CzF40R9XvA [expires_in] => 7200 [refresh_token] => Iz3olCrkqPBOJvSSH2bOKvA09Sjvsp1c8Ltm7MvxxPfQXSbvI_WoVmzhjqASzwlMa7TAGgsg3mIJmaHjL7jrJHDqUF1jKbhd6GNDnLtXq0U [openid] => ota_XwQ4r_5nioVmshQ [scope] => snsapi_userinfo ) [userinfo] => Array ( [openid] => ota_XwQ4r_5nioVmshQq [nickname] => 野狐 [sex] => 1 [language] => zh_CN [city] => 杭州 [province] => 浙江 [country] => 中國(guó) [headimgurl] => http://wx.qlogo.cn/mmopen/PiajxSqBRaELwee7rhrt2ibnkC1MEnu04WiaWrw9FkuPBbGOgnrMbynNoEuxicgXOetW5VqQbTrS4fZDXNvAWsz6GQ/0 [privilege] => Array ( ) ) )
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

ThinkPHP實(shí)現(xiàn)動(dòng)態(tài)包含文件的方法

PHP設(shè)計(jì)模式(三)建造者模式Builder實(shí)例詳解【創(chuàng)建型】

php實(shí)現(xiàn)12306余票查詢、價(jià)格查詢示例

PHP使用Memcache時(shí)模擬命名空間及緩存失效問(wèn)題的解決

PHP生成自定義長(zhǎng)度隨機(jī)字符串的函數(shù)分享

php添加數(shù)據(jù)到xml文件的簡(jiǎn)單例子