首页 > 后端开发 > php教程 > 微信订阅号开发之token验证后,自动回复消息效能做好,发送消息没有返回

微信订阅号开发之token验证后,自动回复消息效能做好,发送消息没有返回

WBOY
发布: 2016-06-13 12:28:04
原创
1058 人浏览过

微信订阅号开发之token验证后,自动回复消息功能做好,发送消息没有返回

相信很多人会跟我一样,token验证之后,发送消息给订阅号,没有消息返回。

以下,说一下我辛苦调试得到的解决办法:

首先,token验证:

自己写的token一直验证失败,找了好久,没有发现bug。实在没办法,就用了官方的示例代码。并且通过示例代码调试,发现了一个让我吐血的bug(也不算bug):

token验证貌似要求字符编码格式!!!!

官方的示例代码,直接上传到服务器,token直接过!

把官方示例代码改为UTF-8格式,再上传覆盖,token失败失败!失败!

后来,把自己写的修改为ANSI格式还是token失败!醉了醉了!那只好用官方示例代码。在此,说下,token是一次握手验证,验证过一次就不用了。

 

下面,言归正传,貌似偏题了...orz

token验证之后,直接用官方示例代码,赶紧测试自己的订阅号,结果....发出去的消息就跟泼出去的水一样,什么鬼都没有返回...orz

又各种找bug,各种群问,各种搜索....历经本博主九九八十一的努力,终于找出了问题所在(这里是指我自己开发的,并不包括全部,如果你有不同的bug,欢迎交流):

1、最容易被忽视的一个bug,官方给的示例代码,压根就没调用写好的那个responseMsg()函数!

2、把之前的token代码注释,也就是$wechatObj->valid();这行代码。因为toke验证那段代码会有一个echo $echostr,会把responseMsg()函数里的echo $resultStr;(56行)xml格式混乱,输回给微信服务器就无法识别了(貌似只能识别xml格式,还有json格式)。(token验证是一次握手验证,验证开发者之后,就可以不用了,赶紧让它消失在我们整洁的代码orz...)

3、最恶心的一个bug,还是字符编码问题!orz...xml要求UTF-8编码,所以,把示例代码改回UTF-8编码!这个bug找的让我崩溃!!!

 

下面是我修改后的代码,能正常运行,无bug,需要的可以参考一下

<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><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></xml></span><span style="color: #008080;">44</span> <span style="color: #000000;">                            <tousername></tousername></span><span style="color: #008080;">45</span> <span style="color: #000000;">                            <fromusername></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></msgtype></span><span style="color: #008080;">48</span> <span style="color: #000000;">                            <content></content></span><span style="color: #008080;">49</span> <span style="color: #000000;">                            <funcflag>0</funcflag></span><span style="color: #008080;">50</span>                             "<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> ?>
登录后复制

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板