Basic configuration of WeChat public address

Today, I configured the token in the afternoon, but I don't know what's wrong. I found many methods on the Internet and didn't solve the problem, but I finally found a solution in a forum. I'm really excited. Don't say no more. Just connect the code.

This is the configuration of wechat public platform.

  

The url value here should be directly pointed to the file, otherwise it will not succeed

Then the background settings

 1 <?php
 2 namespace app\weixin\controller;
 3 
 4 use think\Controller;
 5 
 6 define("TOKEN", "******");//Here you need to enter your token value
 7 
 8 // $wechatObj = new wechatCallbackapiTest();
 9 
10 // $wechatObj->valid();
11 
12 class Wx2 extends Controller {
13     
14     public function index()  
15     {  
16         $echoStr = $_GET["echostr"];  
17   
18         //valid signature , option  
19         if($this->checkSignature()){  
20             ob_clean(); //Discard cached content
21             echo $echoStr;  
22             exit;  
23         }  
24     }  
25   
26     public function responseMsg()  
27     {  
28         //get post data, May be due to the different environments  
29         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
30   
31         //extract post data  
32         if (!empty($postStr)){  
33                   
34                 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  
35                 $fromUsername = $postObj->FromUserName;  
36                 $toUsername = $postObj->ToUserName;  
37                 $keyword = trim($postObj->Content);  
38                 $time = time();  
39                 $textTpl = "<xml>  
40                             <ToUserName><![CDATA[%s]]></ToUserName>  
41                             <FromUserName><![CDATA[%s]]></FromUserName>  
42                             <CreateTime>%s</CreateTime>  
43                             <MsgType><![CDATA[%s]]></MsgType>  
44                             <Content><![CDATA[%s]]></Content>  
45                             <FuncFlag>0</FuncFlag>  
46                             </xml>";               
47                 if(!empty( $keyword ))  
48                 {  
49                     $msgType = "text";  
50                     $contentStr = "Welcome to wechat world!";  
51                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
52                     echo $resultStr;  
53                 }else{  
54                     echo "Input something...";  
55                 }  
56   
57         }else {  
58             echo "";  
59             exit;  
60         }  
61     }  
62           
63     private function checkSignature()  
64     {  
65         $signature = $_GET["signature"];  
66         $timestamp = $_GET["timestamp"];  
67         $nonce = $_GET["nonce"];      
68                   
69         $token = TOKEN;  
70         $tmpArr = array($token, $timestamp, $nonce);  
71         sort($tmpArr);  
72         $tmpStr = implode( $tmpArr );  
73         $tmpStr = sha1( $tmpStr );  
74           
75         if( $tmpStr == $signature ){  
76             return true;  
77         }else{  
78             return false;  
79         }  
80     }
81 
82 
83 }

If this method doesn't work, you have to modify the coding method to implement it. There are many ways to modify the coding method on the Internet, and you can search them by yourself,

I'll share it here today, and then I think what I wrote is good. Please recommend it. Thank you

Keywords: PHP xml SHA1

Added by jokerfool on Sun, 05 Jan 2020 19:17:44 +0200