About PHP WeChat subscription number development token verification automatically send a message to the subscription number but no message returned the issue

Source: Internet
Author: User
Tags cdata
I believe a lot of people will be like me. After token authentication, the message is sent to the subscription number, and no message is returned.

Here are some of the solutions I have worked hard to debug:

First, token verification:

My own write token has been failed to verify, looking for a long time, no bug found. There is no way to use the official sample code. and by debugging the sample code, I found a bug that made me spit blood (and not a bug):

Token verification seems to require character encoding format!!!

Official sample code, directly uploaded to the server, token directly over!

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

Later, to write their own changes to ANSI format or token failed! Drunk, Drunk! That had to be the official sample code. In this, the next, token is a handshake verification, verified once is not used.

Below, to the next, it seems tricky ... Orz

After token verification, directly with the official sample code, hurriedly test their subscription number, the results .... The message was sent out like spilled water, and nothing was returned ... Orz

And all kinds of bugs, various groups of questions, various search .... After the efforts of the blogger 9,981, finally found out the problem (here refers to my own development, does not include all, if you have different bugs, welcome communication):

1, the most easily overlooked a bug, the official sample code, did not call the written responsemsg () function!

2, the previous token code comments, that is $wechatobj->valid (); This line of code. Because Toke verifies that the code will have an echo $echostr, the responsemsg () function in the Echo $resultStr, (56 lines) XML format confusion, the return to the server is not recognized (seemingly only to recognize the XML format, and JSON format). (token verification is a handshake verification, after verifying the developer, you can not, hurriedly let it disappear in our neat code Orz ... )

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

Below is my revised code, can run normally, no bug, need to refer to

<?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, could 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 Injecti     On, the best of are 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 = "
 ";      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 () {//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 small part of the PHP subscription number to share the development of token verification automatically send a message to the subscription number but no message back to the solution, I hope you like.

The above describes the PHP subscription number development token verification automatically send a message to the subscription number but no message returned the issue, including the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.