1,建一個vendor類代碼如下
<?php class Wxlogin { # 你自己的 private $app_id = ''; # 也是你自己的 private $app_secret = ''; /** * # +======================================================================== * # | - @name 獲取微信授權鏈接 * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-07 * # +------------------------------------------------------------------------ * # | - 1.獲取微信授權鏈接 * # +======================================================================== */ public function get_authorize_url($redirect_uri = '', $state = ''){ $redirect_uri = urlencode($redirect_uri); return "https://open.weixin.qq.com/connect/qrconnect?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect"; } /** * # +======================================================================== * # | - @name 獲取授權token * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-07 * # +------------------------------------------------------------------------ * # | - 1.通過get_authorize_url獲取到的code * # +======================================================================== */ public function get_access_token($code = ''){ $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code"; $token_data = $this->http($token_url); if($token_data[0] == 200){ return json_decode($token_data[1], TRUE); } return FALSE; } /** * # +======================================================================== * # | - @name 獲取授權后的微信用戶信息 * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-07 * # +------------------------------------------------------------------------ * # | - 1.獲取授權后的微信用戶信息 * # +======================================================================== */ public function get_user_info($access_token = '', $open_id = ''){ if($access_token && $open_id){ $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN"; $info_data = $this->http($info_url); if($info_data[0] == 200){ return json_decode($info_data[1], TRUE); } } return FALSE; } /** * # +======================================================================== * # | - @name 驗證授權 * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-07 * # +------------------------------------------------------------------------ * # | - 1.驗證授權 * # +======================================================================== */ public function check_access_token($access_token = '', $open_id = ''){ if($access_token && $open_id){ $info_url = "https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$open_id}&lang=zh_CN"; $info_data = $this->http($info_url); if($info_data[0] == 200){ return json_decode($info_data[1], TRUE); } } return FALSE; } /** * # +======================================================================== * # | - @name 請求 * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-07 * # +------------------------------------------------------------------------ * # | - 1.請求 * # +======================================================================== */ public function http($url, $method, $postfields = null, $headers = array()){ $ci = curl_init(); curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, true); if (!empty($postfields)) { curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); $this->postdata = $postfields; } break; } curl_setopt($ci, CURLOPT_URL, $url); curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLINFO_HEADER_OUT, true); $response = curl_exec($ci); $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); curl_close($ci); return array($http_code, $response); } }
html頁面如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <title>掃碼登錄</title> <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div id="wxqrcode"></div> </body> <script type="text/javascript"> var obj = new WxLogin({ self_redirect:false, id:"wxqrcode", appid: "你的appid", scope: "snsapi_login", redirect_uri: "授權域名下的頁面,掃碼授權后會跳轉", state: "隨機加密MD5就行了",//自己存session style: "black或者white", // 加密之后的樣式,要改自己解密去 href: "data:text/css;base64,LmltcG93ZXJCb3ggLnFyY29kZSB7d2lkdGg6IDIwMHB4O30NCi5pbXBvd2VyQm94IC50aXRsZSB7ZGlzcGxheTogbm9uZTt9DQouaW1wb3dlckJveCAuaW5mbyB7d2lkdGg6IDIwMHB4O30NCi5zdGF0dXNfaWNvbiB7ZGlzcGxheTogbm9uZX0NCi5pbXBvd2VyQm94IC5zdGF0dXMge3RleHQtYWxpZ246IGNlbnRlcjt9" }); </script> </html>
你的redirect_uri所填下的頁面如下,我以authorization為名
/** * # +======================================================================== * # | - @name 授權處理 * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-07 * # +------------------------------------------------------------------------ * * # +======================================================================== */ public function authorization() { $code = $_GET['code']; $state = $_GET['state']; if(!$code || !$state) die('參數不能為空'); # 驗證參數,防刷 if($state != session('state')){ die('錯誤state'); }
Vendor('Wxlogin.wxlogin'); $Wx = new \Wxlogin(); # 確認授權后會,根據返回的code獲取token $token = $Wx->get_access_token($_GET['code']); # 獲取用戶信息 $user_info = $Wx->get_user_info($token['access_token'],$token['openid']); var_dump($user_info); }
還有第二種方法,自己去看文檔。不懂再問吧 qq137121172 備注微信網頁授權登錄