本文使用php 進行微信pc 掃碼登錄,掃碼獲取用戶資訊
話不多說直接上代碼吧!
怎么使用在代碼最下面
<?php class WeChatRcLogin { public $state = ''; public $appid = ''; public $secret = ''; public $redirect_uri = ''; public $error = ''; public $data = []; public function __construct($config) { $this->state = $config['state']; $this->appid = $config['appid']; $this->secret = $config['secret']; $this->redirect_uri = $config['redirect_uri']; } /** * 獲取登錄的url * @return string * @author: wmq * @Time: 2022/10/13 14:40 */ public function getLoginUrl(){ $arr = [ 'appid' => $this->appid, // appid 'redirect_uri' => $this->redirect_uri, //回呼url 'response_type' => 'code', 'scope' => 'snsapi_login', 'state' => $this->state, //回呼驗簽資料 ]; return 'https://open.weixin.qq.com/connect/qrconnect?'.http_build_query($arr).'#wechat_redirect'; } /** * 獲取access_token * @param $param * @return bool * @author: wmq * @Time: 2022/10/13 14:40 */ public function getAccessToken($param){ if(!isset($param['code'],$param['state'])){ $this->error = '缺少引數'; return false; } $arr = [ 'appid' => $this->appid, 'secret' => $this->secret, 'code' => $param['code'], 'grant_type' => 'authorization_code' ]; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?'.http_build_query($arr); $result = Curl::get($url); if(isset($result['errcode'])){ $this->error = $result['errmsg']; return false; }else{ if($this->state && $param['state'] != $this->state){ $this->error = '驗簽失敗'; return false; } $this->data = https://www.cnblogs.com/quan846951943/p/$result; return true; } } /** * 獲取用戶資訊 * @param $param * @return bool * @author: wmq * @Time: 2022/10/13 14:40 */ public function getUserInfo($param){ if(!isset($param['access_token'],$param['openid'])){ $this->error = '缺少引數'; return false; } $arr = [ 'access_token' => $param['access_token'], 'openid' => $param['openid'], ]; $url = 'https://api.weixin.qq.com/sns/userinfo?'.http_build_query($arr); $result = Curl::get($url); if(isset($result['errcode'])){ $this->error = $result['errmsg']; return false; }else{ $this->data = https://www.cnblogs.com/quan846951943/p/$result; return true; } } } class Curl { /** * curl post 請求 * @param string $url 地址 * @param array $data 資料 * @return bool|string * @author: wmq * @Time: 2022/4/25 9:18 */ public static function post($url, $data = [],$header = []){ return self::curl_request($url,$data,$header); } /** * get請求 * @param string $url 地址 * @param int $timeout 時間 * @return bool|string * @author: wmq * @Time: 2022/4/25 9:21\ */ public static function get($url,$header = []){ return self::curl_request($url,[],$header); } public static function curl_request($url,$data = [],$header) { !$header && $headerArray = ["Content-type:application/json;charset='utf-8'","Accept:application/json"]; $header && $headerArray = $header; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } $output = curl_exec($curl); curl_close($curl); $res = json_decode($output,true); return $res; } } //使用方法 $config = [ 'appid' => '', //appid 微信開放平臺獲取 'secret' => '', // secret 微信開放平臺獲取 'redirect_uri' => '', // 回呼地址,微信開放平臺設定,掃碼成功后微信調到并攜帶code,state的地址 'state' => '' // 回呼簽名驗簽 可以為空,可以存到快取中驗證 ]; $arr = []; $l = new WeChatRcLogin($config); //獲取跳轉地址 // $l->getLoginUrl(); //https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Authorized_Interface_Calling_UnionID.html //access_token 建議根據expires_in 進行快取 獲取access_token 是有次數限制的,明細請看官網檔案 //獲取用戶資訊 if($l->getAccessToken($_GET)){ //獲取access_token 根據state, code 微信回呼會傳過來, $data = $l->data; //access_token 資訊 if($l->getUserInfo($l->data)){ // 獲取用戶資訊 echo '獲取成功<br>'; $arr = array_merge($arr,$l->data); //合并access_token 回傳的東西 + 用戶資訊回傳的東西 var_dump($arr); }else{ //獲取用戶資訊失敗 echo '獲取用戶資訊失敗<br>'; var_dump($l->error); } }else{ echo '獲取access_token失敗<br>'; var_dump($l->error); echo '<br>'; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518802.html
標籤:PHP
下一篇:字典的創建方式
