1,建一個vendor類代碼以下javascript
<?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頁面以下php
<!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爲名css
/** * # +======================================================================== * # | - @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 備註微信網頁受權登陸html