Home php教程 php手册 After the token verification of WeChat subscription account development, the automatic reply message function is ready and the message is sent.

After the token verification of WeChat subscription account development, the automatic reply message function is ready and the message is sent.

Jul 06, 2016 pm 01:30 PM
token reply WeChat information automatic subscription verify

I believe many people will be like me. After the token is verified, a message is sent to the subscription account, but no message is returned. Below, let me talk about the solution I got through hard debugging: First, token verification: The token I wrote has always failed to verify. I searched for it for a long time, but no bug was found. There was really no other way, so I used the official sample code. And through debugging the sample code, I found

I believe many people will be like me. After the token is verified, a message is sent to the subscription account, but no message is returned.

The following is the solution I got through hard debugging:

First, token verification:

The token I wrote has always failed to verify. I have been looking for it for a long time, but no bug has been found. There was really no other way, so I used the official sample code. And through debugging the sample code, I found a bug that made me vomit blood (not a bug):

Token verification seems to require character encoding format! ! ! !

The official sample code is uploaded directly to the server, and the token is passed directly!

Changed the official sample code to UTF-8 format, and then uploaded it to overwrite it. Token failed! Failed! fail!

Later, I changed what I wrote to ANSI format and the token still failed! Drunk, drunk! Then you have to use the official sample code. Here, let me say that token is a one-time handshake verification , and it is no longer needed after verification once.

Now, let’s get back to the topic, I seem to have digressed...orz

After the token verification, I directly used the official sample code to quickly test my subscription account. As a result... the message sent out was like water poured out, and nothing was returned...orz

I searched for bugs in various ways, asked in various groups, searched in various ways... After the efforts of this blogger, I finally found out the problem (here it refers to the one I developed myself, not the one I developed myself). Including all, if you have different bugs, welcome to communicate):

1. The most easily overlooked bug is that the official sample code does not call the written responseMsg() function at all!

2. Comment out the previous token code, which is the line of code $wechatObj->valid();. Because there will be an echo $echostr in the toke verification code, the echo $resultStr; (line 56) in the responseMsg() function will be in a confusing xml format, and it will not be recognized when it is input back to the WeChat server (it seems Only xml format and json format can be recognized). (Token verification is a handshake verification. After verifying the developer, it is no longer needed. Let it disappear in our neat code orz...)

3. The most disgusting bug is the character encoding problem! orz...xml requires UTF-8 encoding, so, change the sample code back to UTF-8 encoding! This bug makes me collapse! ! !

The following is my modified code. It can run normally and has no bugs. You can refer to it if you need it

<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">*
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">  * wechat php test
</span><span style="color: #008080;"> 4</span>   <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 5</span> 
<span style="color: #008080;"> 6</span> <span style="color: #008000;">//</span><span style="color: #008000;">define your token</span>
<span style="color: #008080;"> 7</span> <span style="color: #008080;">define</span>("TOKEN", "codcodog"<span style="color: #000000;">);
</span><span style="color: #008080;"> 8</span> 
<span style="color: #008080;"> 9</span> <span style="color: #800080;">$wechatObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> wechatCallbackapiTest();
</span><span style="color: #008080;">10</span> <span style="color: #008000;">//</span><span style="color: #008000;">$wechatObj->valid();
<span style="color: #008080;">11</span> <span style="color: #800080;">$wechatObj</span>-><span style="color: #000000;">responseMsg();
</span><span style="color: #008080;">12</span> 
<span style="color: #008080;">13</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> wechatCallbackapiTest
</span><span style="color: #008080;">14</span> <span style="color: #000000;">{
</span><span style="color: #008080;">15</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> valid()
</span><span style="color: #008080;">16</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">17</span>         <span style="color: #800080;">$echoStr</span> = <span style="color: #800080;">$_GET</span>["echostr"<span style="color: #000000;">];
</span><span style="color: #008080;">18</span> 
<span style="color: #008080;">19</span>         <span style="color: #008000;">//</span><span style="color: #008000;">valid signature , option</span>
<span style="color: #008080;">20</span>         <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">checkSignature()){
</span><span style="color: #008080;">21</span>         <span style="color: #008080;">header</span>('content-type:text'<span style="color: #000000;">);
</span><span style="color: #008080;">22</span>             <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$echoStr</span><span style="color: #000000;">;
</span><span style="color: #008080;">23</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;">24</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">25</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">26</span> 
<span style="color: #008080;">27</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> responseMsg()
</span><span style="color: #008080;">28</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">29</span>         <span style="color: #008000;">//</span><span style="color: #008000;">get post data, May be due to the different environments</span>
<span style="color: #008080;">30</span>         <span style="color: #800080;">$postStr</span> = <span style="color: #800080;">$GLOBALS</span>["HTTP_RAW_POST_DATA"<span style="color: #000000;">];
</span><span style="color: #008080;">31</span>         <span style="color: #008000;">//</span><span style="color: #008000;">$postStr = file_get_contents("php://input");</span>
<span style="color: #008080;">32</span>         <span style="color: #008080;">file_put_contents</span>("log.txt",<span style="color: #800080;">$postStr</span>,<span style="color: #000000;">FILE_APPEND );
</span><span style="color: #008080;">33</span>           <span style="color: #008000;">//</span><span style="color: #008000;">extract post data</span>
<span style="color: #008080;">34</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$postStr</span><span style="color: #000000;">)){
</span><span style="color: #008080;">35</span>                 <span style="color: #008000;">/*</span><span style="color: #008000;"> libxml_disable_entity_loader is to PRevent XML eXternal Entity Injection,
</span><span style="color: #008080;">36</span> <span style="color: #008000;">                   the best way is to check the validity of xml by yourself </span><span style="color: #008000;">*/</span>
<span style="color: #008080;">37</span>                 libxml_disable_entity_loader(<span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #008080;">38</span>                   <span style="color: #800080;">$postObj</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$postStr</span>, 'SimpleXMLElement',<span style="color: #000000;"> LIBXML_NOCDATA);
</span><span style="color: #008080;">39</span>                 <span style="color: #800080;">$fromUsername</span> = <span style="color: #800080;">$postObj</span>->FromUserName; <span style="color: #008000;">//</span><span style="color: #008000;">用户</span>
<span style="color: #008080;">40</span>                 <span style="color: #800080;">$toUsername</span> = <span style="color: #800080;">$postObj</span>->ToUserName;     <span style="color: #008000;">//</span><span style="color: #008000;">公众平台</span>
<span style="color: #008080;">41</span>                 <span style="color: #800080;">$keyWord</span> = <span style="color: #008080;">trim</span>(<span style="color: #800080;">$postObj</span>-><span style="color: #000000;">Content);
</span><span style="color: #008080;">42</span>                 <span style="color: #800080;">$time</span> = <span style="color: #008080;">time</span><span style="color: #000000;">();
</span><span style="color: #008080;">43</span>                 <span style="color: #800080;">$textTpl</span> = "<span style="color: #000000;"><xml>
</span><span style="color: #008080;">44</span> <span style="color: #000000;">                            <ToUserName><![CDATA[%s]]></ToUserName>
</span><span style="color: #008080;">45</span> <span style="color: #000000;">                            <FromUserName><![CDATA[%s]]></FromUserName>
</span><span style="color: #008080;">46</span> <span style="color: #000000;">                            <CreateTime>%s</CreateTime>
</span><span style="color: #008080;">47</span> <span style="color: #000000;">                            <MsgType><![CDATA[%s]]></MsgType>
</span><span style="color: #008080;">48</span> <span style="color: #000000;">                            <Content><![CDATA[%s]]></Content>
</span><span style="color: #008080;">49</span> <span style="color: #000000;">                            <FuncFlag>0</FuncFlag>
</span><span style="color: #008080;">50</span>                             </xml>"<span style="color: #000000;">;             
</span><span style="color: #008080;">51</span>                 <span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>( <span style="color: #800080;">$keyword</span><span style="color: #000000;"> ))
</span><span style="color: #008080;">52</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">53</span>                       <span style="color: #800080;">$msgType</span> = "text"<span style="color: #000000;">;
</span><span style="color: #008080;">54</span>                     <span style="color: #800080;">$contentStr</span> = "Welcome to wechat world!"<span style="color: #000000;">;
</span><span style="color: #008080;">55</span>                     <span style="color: #800080;">$resultStr</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$textTpl</span>, <span style="color: #800080;">$fromUsername</span>, <span style="color: #800080;">$toUsername</span>, <span style="color: #800080;">$time</span>, <span style="color: #800080;">$msgType</span>, <span style="color: #800080;">$contentStr</span><span style="color: #000000;">);
</span><span style="color: #008080;">56</span>                     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$resultStr</span><span style="color: #000000;">;
</span><span style="color: #008080;">57</span>                 }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">58</span>                     <span style="color: #0000ff;">echo</span> "Input something..."<span style="color: #000000;">;
</span><span style="color: #008080;">59</span> <span style="color: #000000;">                }
</span><span style="color: #008080;">60</span> 
<span style="color: #008080;">61</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;">62</span>             <span style="color: #0000ff;">echo</span> ""<span style="color: #000000;">;
</span><span style="color: #008080;">63</span>             <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;">64</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">65</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">66</span>         
<span style="color: #008080;">67</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> checkSignature()
</span><span style="color: #008080;">68</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">69</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> you must define TOKEN by yourself</span>
<span style="color: #008080;">70</span>         <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">defined</span>("TOKEN"<span style="color: #000000;">)) {
</span><span style="color: #008080;">71</span>             <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>('TOKEN is not defined!'<span style="color: #000000;">);
</span><span style="color: #008080;">72</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">73</span>         
<span style="color: #008080;">74</span>         <span style="color: #800080;">$signature</span> = <span style="color: #800080;">$_GET</span>["signature"<span style="color: #000000;">];
</span><span style="color: #008080;">75</span>         <span style="color: #800080;">$timestamp</span> = <span style="color: #800080;">$_GET</span>["timestamp"<span style="color: #000000;">];
</span><span style="color: #008080;">76</span>         <span style="color: #800080;">$nonce</span> = <span style="color: #800080;">$_GET</span>["nonce"<span style="color: #000000;">];
</span><span style="color: #008080;">77</span>                 
<span style="color: #008080;">78</span>         <span style="color: #800080;">$token</span> =<span style="color: #000000;"> TOKEN;
</span><span style="color: #008080;">79</span>         <span style="color: #800080;">$tmpArr</span> = <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$token</span>, <span style="color: #800080;">$timestamp</span>, <span style="color: #800080;">$nonce</span><span style="color: #000000;">);
</span><span style="color: #008080;">80</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> use SORT_STRING rule</span>
<span style="color: #008080;">81</span>         <span style="color: #008080;">sort</span>(<span style="color: #800080;">$tmpArr</span>,<span style="color: #000000;"> SORT_STRING);
</span><span style="color: #008080;">82</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">implode</span>( <span style="color: #800080;">$tmpArr</span><span style="color: #000000;"> );
</span><span style="color: #008080;">83</span>         <span style="color: #800080;">$tmpStr</span> = <span style="color: #008080;">sha1</span>( <span style="color: #800080;">$tmpStr</span><span style="color: #000000;"> );
</span><span style="color: #008080;">84</span>         
<span style="color: #008080;">85</span>         <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$tmpStr</span> == <span style="color: #800080;">$signature</span><span style="color: #000000;"> ){
</span><span style="color: #008080;">86</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">87</span>         }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">88</span>             <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">89</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">90</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">91</span> <span style="color: #000000;">}
</span><span style="color: #008080;">92</span> 
<span style="color: #008080;">93</span> 
<span style="color: #008080;">94</span> ?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

What are the development tools for H5 and mini program? What are the development tools for H5 and mini program? Apr 06, 2025 am 09:54 AM

H5 development tools recommendations: VSCode, WebStorm, Atom, Brackets, Sublime Text; Mini Program Development Tools: WeChat Developer Tools, Alipay Mini Program Developer Tools, Baidu Smart Mini Program IDE, Toutiao Mini Program Developer Tools, Taro.

Detailed tutorial on how to buy and sell Binance virtual currency Detailed tutorial on how to buy and sell Binance virtual currency Mar 18, 2025 pm 01:36 PM

This article provides a brief guide to buying and selling of Binance virtual currency updated in 2025, and explains in detail the operation steps of virtual currency transactions on the Binance platform. The guide covers fiat currency purchase USDT, currency transaction purchase of other currencies (such as BTC), and selling operations, including market trading and limit trading. In addition, the guide also specifically reminds key risks such as payment security and network selection for fiat currency transactions, helping users to conduct Binance transactions safely and efficiently. Through this article, you can quickly master the skills of buying and selling virtual currencies on the Binance platform and reduce transaction risks.

See all articles