Heim >Backend-Entwicklung >PHP-Tutorial >微信订阅号开发之token验证后,自动回复消息功能做好,发送消息没有返回,token自动回复_PHP教程
相信很多人会跟我一样,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> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span> * wechat php test </span><span> 4</span> <span>*/</span> <span> 5</span> <span> 6</span> <span>//</span><span>define your token</span> <span> 7</span> <span>define</span>("TOKEN", "codcodog"<span>); </span><span> 8</span> <span> 9</span> <span>$wechatObj</span> = <span>new</span><span> wechatCallbackapiTest(); </span><span>10</span> <span>//</span><span>$wechatObj->valid();</span> <span>11</span> <span>$wechatObj</span>-><span>responseMsg(); </span><span>12</span> <span>13</span> <span>class</span><span> wechatCallbackapiTest </span><span>14</span> <span>{ </span><span>15</span> <span>public</span> <span>function</span><span> valid() </span><span>16</span> <span> { </span><span>17</span> <span>$echoStr</span> = <span>$_GET</span>["echostr"<span>]; </span><span>18</span> <span>19</span> <span>//</span><span>valid signature , option</span> <span>20</span> <span>if</span>(<span>$this</span>-><span>checkSignature()){ </span><span>21</span> <span>header</span>('content-type:text'<span>); </span><span>22</span> <span>echo</span> <span>$echoStr</span><span>; </span><span>23</span> <span>exit</span><span>; </span><span>24</span> <span> } </span><span>25</span> <span> } </span><span>26</span> <span>27</span> <span>public</span> <span>function</span><span> responseMsg() </span><span>28</span> <span> { </span><span>29</span> <span>//</span><span>get post data, May be due to the different environments</span> <span>30</span> <span>$postStr</span> = <span>$GLOBALS</span>["HTTP_RAW_POST_DATA"<span>]; </span><span>31</span> <span>//</span><span>$postStr = file_get_contents("php://input");</span> <span>32</span> <span>file_put_contents</span>("log.txt",<span>$postStr</span>,<span>FILE_APPEND ); </span><span>33</span> <span>//</span><span>extract post data</span> <span>34</span> <span>if</span> (!<span>empty</span>(<span>$postStr</span><span>)){ </span><span>35</span> <span>/*</span><span> libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, </span><span>36</span> <span> the best way is to check the validity of xml by yourself </span><span>*/</span> <span>37</span> libxml_disable_entity_loader(<span>true</span><span>); </span><span>38</span> <span>$postObj</span> = <span>simplexml_load_string</span>(<span>$postStr</span>, 'SimpleXMLElement',<span> LIBXML_NOCDATA); </span><span>39</span> <span>$fromUsername</span> = <span>$postObj</span>->FromUserName; <span>//</span><span>用户</span> <span>40</span> <span>$toUsername</span> = <span>$postObj</span>->ToUserName; <span>//</span><span>公众平台</span> <span>41</span> <span>$keyword</span> = <span>trim</span>(<span>$postObj</span>-><span>Content); </span><span>42</span> <span>$time</span> = <span>time</span><span>(); </span><span>43</span> <span>$textTpl</span> = "<span><xml> </span><span>44</span> <span> <ToUserName><![CDATA[%s]]></ToUserName> </span><span>45</span> <span> <FromUserName><![CDATA[%s]]></FromUserName> </span><span>46</span> <span> <CreateTime>%s</CreateTime> </span><span>47</span> <span> <MsgType><![CDATA[%s]]></MsgType> </span><span>48</span> <span> <Content><![CDATA[%s]]></Content> </span><span>49</span> <span> <FuncFlag>0</FuncFlag> </span><span>50</span> </xml>"<span>; </span><span>51</span> <span>if</span>(!<span>empty</span>( <span>$keyword</span><span> )) </span><span>52</span> <span> { </span><span>53</span> <span>$msgType</span> = "text"<span>; </span><span>54</span> <span>$contentStr</span> = "Welcome to wechat world!"<span>; </span><span>55</span> <span>$resultStr</span> = <span>sprintf</span>(<span>$textTpl</span>, <span>$fromUsername</span>, <span>$toUsername</span>, <span>$time</span>, <span>$msgType</span>, <span>$contentStr</span><span>); </span><span>56</span> <span>echo</span> <span>$resultStr</span><span>; </span><span>57</span> }<span>else</span><span>{ </span><span>58</span> <span>echo</span> "Input something..."<span>; </span><span>59</span> <span> } </span><span>60</span> <span>61</span> }<span>else</span><span> { </span><span>62</span> <span>echo</span> ""<span>; </span><span>63</span> <span>exit</span><span>; </span><span>64</span> <span> } </span><span>65</span> <span> } </span><span>66</span> <span>67</span> <span>private</span> <span>function</span><span> checkSignature() </span><span>68</span> <span> { </span><span>69</span> <span>//</span><span> you must define TOKEN by yourself</span> <span>70</span> <span>if</span> (!<span>defined</span>("TOKEN"<span>)) { </span><span>71</span> <span>throw</span> <span>new</span> <span>Exception</span>('TOKEN is not defined!'<span>); </span><span>72</span> <span> } </span><span>73</span> <span>74</span> <span>$signature</span> = <span>$_GET</span>["signature"<span>]; </span><span>75</span> <span>$timestamp</span> = <span>$_GET</span>["timestamp"<span>]; </span><span>76</span> <span>$nonce</span> = <span>$_GET</span>["nonce"<span>]; </span><span>77</span> <span>78</span> <span>$token</span> =<span> TOKEN; </span><span>79</span> <span>$tmpArr</span> = <span>array</span>(<span>$token</span>, <span>$timestamp</span>, <span>$nonce</span><span>); </span><span>80</span> <span>//</span><span> use SORT_STRING rule</span> <span>81</span> <span>sort</span>(<span>$tmpArr</span>,<span> SORT_STRING); </span><span>82</span> <span>$tmpStr</span> = <span>implode</span>( <span>$tmpArr</span><span> ); </span><span>83</span> <span>$tmpStr</span> = <span>sha1</span>( <span>$tmpStr</span><span> ); </span><span>84</span> <span>85</span> <span>if</span>( <span>$tmpStr</span> == <span>$signature</span><span> ){ </span><span>86</span> <span>return</span> <span>true</span><span>; </span><span>87</span> }<span>else</span><span>{ </span><span>88</span> <span>return</span> <span>false</span><span>; </span><span>89</span> <span> } </span><span>90</span> <span> } </span><span>91</span> <span>} </span><span>92</span> <span>93</span> <span>94</span> ?>