Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 08:49:32
Original
1553 people have browsed it

微信订阅号开发之token验证后,自动回复消息功能做好,发送消息没有返回,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,需要的可以参考一下

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

 

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!