WeChat applet (10) message push configuration (token verification failed solution)

Background: WeChat applet development, ready to use template messages to do something, but found that we need to configure in the development of WeChat public platform - development settings - message push, and then our background staff will start various configurations, but once the verification The token reported an error, which was very frustrating. After various investigations, the final reason was found. The process and code are as follows:

The server code given by many websites is as follows:

//检查签名
     public function checkSignature(Request $request) {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $echostr = $request->echostr;
        $token = config('wechat.token');
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if($tmpStr == $signature){

            echo $echostr;
            exit();
            // return true;
        }else{

            return false;
        }
     }

The above code seems to be ok, and it can be used to check the log in the background, but once submitted, the token verification fails, and the background checks various data, and finally finds that it needs to be usedob_clean();最终代码如下:

//检查签名
     public function checkSignature(Request $request) {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $echostr = $request->echostr;
        $token = config('wechat.token');
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if($tmpStr == $signature){

            ob_clean();
            echo $echostr;
            exit();
            // return true;
        }else{

            return false;
        }
     }

The above records the pitfalls encountered in the first step of template message push on the WeChat public platform, and then follow up to record the steps of template message push. I hope it will be helpful to you. Welcome to leave a message and like it. It is best to pay attention.

Guess you like

Origin blog.csdn.net/wh_xmy/article/details/99956691