Unauthorized access docking micro-channel interfaces _tp3.2

Reference documents:
  1. tp5 micro-channel development (a) ---- public micro-channel number configured token https://blog.csdn.net/qq_27987023/article/details/82861521
  2. Micro-channel public number token validation failed Solution - PHP https://blog.csdn.net/u012729832/article/details/79754095
  3. Based on micro-channel micro OAuth2.0 page letter of authorization ThinkPHP3.2.3, micro-channel public number login page, users get basic information https://blog.csdn.net/longgeaisisi/article/details/86077364
 
 Problems encountered:
  2019/06/27
  1. Add the test micro-channel is not url / token verification failure
 1 <?php
 2 namespace Home\Controller;
 3 use Home\Controller\ShareController;
 4 /**
 5   * wechat授权登录
 6   */
 7 
 8 //define your token
 9 define("TOKEN", "qiu37");
10 
11 class WechatcallbackapiController extends ShareController{
12     public function __construct() {
13         parent::__construct();
14         $this->user = D('me');
15         the this $ -> for appId = 'wx6b6021a20a024697' ;
 16          $ the this -> appsecret = 'ac12bf81db7992b4935f32082079af13' ;
 . 17          $ the this -> Valid ();
 18 is      }
 . 19  
20 is  
21 is      // micro unauthorized access channel 
22 is      public  function wxLogin () {
 23 is          // callback URL 
24          $ redirect_uri = urlencode ( 'http://m68cna.natappfree.cc/pow/home/Wechatcallbackapi/getUserInfo' );
 25          // jump redirect_uri to obtain micro-channel callback code 
26 is          $ URL = "HTTPS: // open.weixin.qq.com/connect/oauth2/authorize?appid= ".$this->appId."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
27 
28         header('Location:'.$url);
29     }
30 
31     //获取用户信息
32     public function getUserInfo(){
33         $code = $_GET["code"];
34         $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appId."&secret=".$this->appSecret."&code=".$code."&grant_type=authorization_code";
35         $res = $this->sendRequest($url);
36         $access_token = $res["access_token"];
37         $openId  = $res['openid'];
38         $getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openId&lang=zh_CN";
39         $user_info = $this->sendRequest($getUserInfo);
40         if ($userInfo = $this->user->getUserByOpenid($user_info['openid'])){
41             session('currentUserId',$userInfo['id']);
42             return $this->ajaxSuccess('登录成功',0);
43         }else{
44             unset($user_info['privilege']);
45             $res = $this->user->saveWechatUser($user_info);
46             session('currentUserId',$userInfo['id']);
47             if ( $res ){
48                 return the this $ -> ajaxSuccess ( 'successful login', 0 );
 49              } the else {
 50                  return  $ the this -> ajaxError ( 'login failed', 3 );
 51              }
 52          }
 53 is  
54 is  
55      }
 56 is  
57 is      // transmission request 
58      public  function the sendRequest ( $ URL ) {
 59          $ CH = curl_init ();
 60          curl_setopt ( $ CH , CURLOPT_URL to, $ URL );
 61 is          curl_setopt ( $ CH, CURLOPT_SSL_VERIFYPEER, FALSE);
62         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
63         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
64         $output = curl_exec($ch);
65         curl_close($ch);
66         return json_decode($output, true);
67     }
68 
69     //微信验证
70     public function valid()
71     {
72         $echoStr = $_GET["echostr"];
73         //valid signature , option
74         if ($this->checkSignature()) {
75             echo $echoStr;
76             exit;
77         }
78     }
79 
80     //检查微信签名
81     private function checkSignature(){
82         $signature = $_GET["signature"];
83         $timestamp = $_GET["timestamp"];
84         $nonce = $_GET["nonce"];
85         $token = TOKEN;
86         $tmpArr = array($token, $timestamp, $nonce);
87         sort($tmpArr);
88         $tmpStr = implode( $tmpArr );
89         $tmpStr = sha1( $tmpStr );
90         if( $tmpStr == $signature ){
91             return true;
92         }else{
93             return false;
94         }
95     }
96 }

 

 
 
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/qiusanqi/p/11819365.html