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.
Now, let me talk about the solution I got through hard debugging:
First, token verification:
The token I wrote has always failed to verify. After searching for a long time, 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 a bug that made me vomit blood (not a bug):
Token verification seems to require a character encoding format! ! ! !
The official sample code is directly uploaded 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, but the token failed! fail! fail!
Later, I changed what I wrote to ANSI format but the token still failed! Drunk, drunk! Then you have to use the official sample code. Here, let me say that the token is a one-time handshake verification, and it is no longer needed after verification once.
Now, back to the topic, I seem to have digressed... After verifying the orz
token, 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. , nothing returned... orz
and searched for bugs, asked in various groups, searched... After ninety-nine and eighty-one efforts, the blogger finally found out the problem ( This refers to the ones I developed myself, and does not include all of them. If you have different bugs, please feel free to communicate):
1. The most easily overlooked bug, the official sample code does not call the written code at all. That responseMsg() function!
2. Comment out the previous token code, which is the line $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 that it can only recognize the xml format, Also in json format). (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 example 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
<?php /** * wechat php test */ //define your token define("TOKEN", "codcodog"); $wechatObj = new wechatCallbackapiTest(); //$wechatObj->valid(); $wechatObj->responseMsg(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ header('content-type:text'); echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //$postStr = file_get_contents("php://input"); file_put_contents("log.txt",$postStr,FILE_APPEND ); //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; //用户 $toUsername = $postObj->ToUserName; //公众平台 $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag></FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
The above is the php WeChat subscription account development that the editor shared with you automatically after token verification The solution for sending a message to a subscription account but no message is returned. I hope you like it.
The above introduces the problem of automatically sending messages to the subscription account after token verification but no message is returned in the development of PHP WeChat subscription account, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.