PHP 微信网页授权获取用户信息
生活随笔
收集整理的這篇文章主要介紹了
PHP 微信网页授权获取用户信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近用到過微信用戶授權獲取用戶信息的功能,在這里記錄一下。
因為用戶授權要用到認證過的服務號才有權限,而線上正在使用公眾號,而開發就有些不方便了,這里可以申請一個微信公眾測試號。
1.申請一個微信公眾測試號
2.配置測試號公網服務器地址
token 可以自己指定,URL就是網站指定地址進行token驗證,驗證通過就可以使用服務器了
驗證代碼如下:
#控制器/*** 微信Token 驗證*/public function checkSignature(){$signature = request()->input('signature');$timestamp = request()->input('timestamp');$nonce = request()->input('nonce');$echostr = request()->input('echostr');echo $this->WeChat->checkSignature($signature, $timestamp, $nonce, $echostr);}#模型層/*** 微信驗證簽名* @param $signature* @param $timestamp* @param $nonce* @param $echostr* @return bool*/public function checkSignature($signature, $timestamp, $nonce, $echostr){if (empty($signature) || empty($timestamp) || empty($nonce)) return false;$token = self::$TOKEN; // 網頁上設置的token$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr, SORT_STRING);$tmpStr = implode($tmpArr);$tmpStr = sha1($tmpStr);if ($tmpStr == $signature) {return $echostr;} else {return false;}}#路由 Route::any('checkSignature', 'Controller@checkSignature');3.關注測試號后就可以使用關注的微信號打開授權的頁面
4.修改網頁授權回調域名
5.網頁授權流程分為四步:
(1)引導用戶進入授權頁面同意授權,獲取code
(2)通過code換取網頁授權access_token(與基礎支持中的access_token不同)
(3)如果需要,開發者可以刷新網頁授權access_token,避免過期(可以忽略)
(4)通過網頁授權access_token和openid獲取用戶基本信息(支持UnionID機制)
6.具體實現代碼
(1)路由
//微信網頁授權調試 Route::get('getWeChatCode', 'Controller@getWeChatCode'); //獲取微信回調信息 Route::get('weChatCallback', 'Controller@weChatCallback');?(2)控制器
/*** 微信網頁授權調試*/public function getWeChatCode(){$object = new WeChat();$callback_url = request()->input('url');$res = $object->getCodeUrl($callback_url);echo "<a href='" . $res . "'>點擊</a>";}/*** 獲取用戶信息*/public function weChatCallback(){$code = request()->input('code');$object = new WeChat();$openInfo = $object->getOpenId($code);if (isset($openInfo['errcode'])) {echo '微信授權登錄登錄失敗' . $openInfo['errmsg'];die;}$userInfo = $object->getUserInfo($openInfo['access_token'], $openInfo['openid']);echo '登錄用戶信息:';print_r($userInfo);}?(3)微信方法
<?phpclass WeChat {/*** 微信公眾平臺微信驗證簽名* @var string*/protected static $TOKEN = 'xxxxxxxx';/*** 微信公眾平臺appid* @var string*/protected static $appId = 'xxxxxxx';/*** 微信公眾平臺app secret* @var string*/protected static $appSecret = 'xxxxxxxx';/*** 微信驗證簽名* @param $signature* @param $timestamp* @param $nonce* @param $echostr* @return bool*/public function checkSignature($signature, $timestamp, $nonce, $echostr){if (empty($signature) || empty($timestamp) || empty($nonce)) return false;$token = self::$TOKEN;$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr, SORT_STRING);$tmpStr = implode($tmpArr);$tmpStr = sha1($tmpStr);if ($tmpStr == $signature) {return $echostr;} else {return false;}}/*** 通過公眾平臺key獲取網頁授權頁面* 可通過回調獲取code參數* @param $callback_url:回調地址* @return string*/public function getCodeUrl($callback_url){$callback = urlencode($callback_url);$AppId = self::$appId;$get_code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$AppId}&redirect_uri={$callback}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";return $get_code_url;}/*** 通過公眾平臺key* 獲取用戶openId access_token* @param $code* @return bool|string*/public function getOpenId($code){$AppId = self::$appId;$AppSecret = self::$appSecret;$get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$AppId}&secret={$AppSecret}&code={$code}&grant_type=authorization_code";$res = file_get_contents($get_openid_url);return djson($res);}/*** 獲取微信用戶信息* @param $access_token* @param $openId* @return bool|mixed*/public function getUserInfo($access_token, $openId){$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openId}&lang=zh_CN";$res = $this->linkCurl($url, 'GET');$res = json_decode($res, true);return $res;}/*** 請求接口返回內容* @param $url :請求的URL地址* @param $method :請求方式POST|GET* @param $params :請求的參數* @param $header : 請求頭* @return bool|string*/protected function linkCurl($url, $method, $params = array(), $header = array()){$ch = curl_init();curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HTTPHEADER, $header);curl_setopt($ch, CURLOPT_FAILONERROR, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);if (strpos("$" . $url, "https://") == 1) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);curl_setopt($ch, CURLOPT_TIMEOUT, 60);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);if ($method == "POST") {curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $params);} else if ($params) {curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));}$response = curl_exec($ch);if ($response === false) {return false;}curl_close($ch);return $response;} }7.編輯訪問路徑,在關注的微信中打開
如果非微信打開:
打開效果
第一次授權顯示頁面為:
多次授權就會顯示以下圖片
得到用戶信息
注意:
code說明 : code作為換取access_token的票據,每次用戶授權帶上的code將不一樣,code只能使用一次,5分鐘未被使用自動過期。
總結
以上是生活随笔為你收集整理的PHP 微信网页授权获取用户信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝牙协议开发常见词汇缩写
- 下一篇: 上班人员必读:“五险一金”详解!