Home > Backend Development > PHP Tutorial > Regarding the problem of automatically sending messages to the subscription account after token verification in PHP WeChat subscription account development but no message is returned, _PHP Tutorial

Regarding the problem of automatically sending messages to the subscription account after token verification in PHP WeChat subscription account development but no message is returned, _PHP Tutorial

WBOY
Release: 2016-07-12 09:02:28
Original
930 people have browsed it

Regarding the problem of automatically sending messages to the subscription account after token verification in php WeChat subscription account development but no message is returned,

I believe many people will be like me. After token verification, Send a message to the subscription account, but no message is returned.

The following is the solution I got through hard debugging:

First, token verification:

The token I wrote has always failed to verify. I have been looking for it for a long time, but no bug has been 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 character encoding format! ! ! !

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

Changed the official sample code to UTF-8 format, and then uploaded the overlay, but the token failed! fail! fail!

Later, I changed what I wrote to ANSI format and 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, let’s get back to the topic, I seem to have digressed...orz

After the token verification, 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, and nothing was returned...orz

I searched for bugs in various ways, asked in various groups, searched in various ways... After the efforts of this blogger, I finally found out the problem (here it refers to the one I developed myself, not the one I developed myself). Including all, if you have different bugs, welcome to communicate):

1. The most easily overlooked bug is that the official sample code does not call the written responseMsg() function at all!

2. Comment out the previous token code, which is the $wechatObj->valid(); line of code. 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 sample 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

<&#63;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;
   }
  }
 }
 &#63;>
Copy after login

The above is the solution that the editor shared with you for the development of PHP WeChat subscription accounts. After token verification, messages are automatically sent to the subscription account but no message is returned. I hope you like it.

Articles you may be interested in:

  • PHP Token (Token) Design
  • PHP Token Token Improved Version
  • Batch processing delims= and tokens =Differences in the use of asterisks
  • Summary of Delims and Tokens in Batch FOR >
  • http://www.bkjia.com/PHPjc/1084581.html
www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/1084581.htmlTechArticle Regarding 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, I believe Many people will be like me. After verifying the token, send a message to...
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