About the PHP micro-credit development of token verification after the automatic sending message to the subscription number but no message returned _php instance

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 messages are returned.

Below, I have been hard to debug the solution:

First, token validation:

Write the token has been failed to verify, looking for a long time, no bugs found. There is no way to use the official sample code. and through sample code debugging, I found a bug that made me vomit blood (and not a bug):

Token authentication appears to require character encoding format!!!!

The official sample code, directly uploaded to the server, token directly over!

Change the official sample code to UTF-8 format, upload overlay, token fail! Failed! Failed!

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

Below, to the bottom of it, seemingly off ... Orz

Token verification, directly with the official sample code, hurriedly test their subscription number, the results .... The message sent out is just like the water pouring out, no ghost has returned ... Orz

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

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 the toke verifies that the code has an echo $echostr, the Echo $resultStr in the responsemsg () function is confused, and the XML format is not recognized by the return to the micro-server (which seems to recognize only XML formats, and JSON format). (token verification is a handshake verification, verify the developer, you can not use, hurriedly let it disappear in our neat code Orz ...) )

3, the most disgusting a bug, or the 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!!!

The following is my modified code, can run normally, no bugs, need to refer to the

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
 $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 The Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOB
   als["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
     tion, 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 = "<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")) {thro W 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 the rule SORT ($TMPARR, sort_string);
   $TMPSTR = implode ($TMPARR);
   $tmpStr = Sha ($TMPSTR);
   if ($tmpStr = = $signature) {return true;
   }else{return false; }}}?>

The above is a small set to share the PHP micro-credit subscriber development of token verification after the automatic send message to the subscription number but no message return solution, I hope you like.

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.