The message is automatically sent to the subscription number after token verification for php WeChat subscription number development, but no message is returned.

Source: Internet
Author: User
: This article mainly introduces the issue that messages are automatically sent to the subscription number after the token verification of php subscription number development but no message is returned. if you are interested in the PHP Tutorial, refer to it. I believe many people will share the same with me. after the token is verified, the message is sent to the subscription number, and no message is returned.

Next, let's talk about the solution I have obtained through hard debugging:

First, verify the token:

The token written by myself failed to be verified. after searching for a long time, no bugs were found. The official sample code is used. Through sample code debugging, I found a bug that makes me vomit blood (not a bug ):

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

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

Change the official sample code to UTF-8 format, and then Upload overwrite, token failed! Failed! Failed!

Later, I failed to change my write to the ANSI format or token! Drunk! So we have to use the official sample code. Here, the token is a handshake verification, and it will not be used once it is verified.

Next, let's get down to the truth. it seems like a problem... orz

After the token is verified, use the official sample code to quickly test your subscription number. The result is .... the Sent message is the same as that of the spilled water. no ghosts are returned... orz

Various bugs, various group questions, various searches .... after the hard work of the blogger on November 11, I finally found out the problem (this refers to my own development, not all. if you have different bugs, please contact us ):

1. the most easily overlooked bug. The official sample code didn't call the prepared responseMsg () function at all!

2. comment out the previous token code, that is, $ wechatObj-> valid (); this line of code. Because the code for toke verification has an echo $ echostr that will confuse the echo $ resultStr; (56 rows) xml format in the responseMsg () function, it cannot be identified when the data is returned to the server (it seems that only xml format and json format can be recognized ). (Token verification is a handshake verification. after verifying the developer, you don't need to use it. hurry and let it disappear into our neat code orz ...)

3. the most disgusting bug is character encoding! Orz... xml requires UTF-8 encoding, so change the sample code back to UTF-8 encoding! This bug caused me to crash !!!

The following is the modified code that can run normally without any bugs. For more information, see

<? 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 () {// ge T 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; // user $ toUsername = $ postObj-> ToUserName; // public platform $ keyword = trim ($ postObj-> Content); $ time = time (); $ textTpl ="
         
  %s
          
  %s
          
  
   
% S
          
  %s
          
  %s
          
          
 "; 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 a solution for automatically sending a message to the subscription number after the token verification developed by the php subscription number is shared with you, but no message is returned.

The above introduces the issue of automatically sending a message to the subscription number after token verification of php subscription number development, but no message is returned, including some content, if you are interested in PHP tutorials.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.