ThinkPHP 微信小程序-消息推送配置 Token校验失败,请检查确认

    public function valid() {
        if (isset($_GET["echostr"])) {
            $echoStr = $_GET["echostr"];
            //valid signature , option
            if (!$this->token) {
                throw new Exception('token is not defined!');
            }
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
            $tmpArr = array($this->token, $timestamp, $nonce);
            // use SORT_STRING rule
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode($tmpArr);
            $tmpStr = sha1($tmpStr);
            if ($tmpStr == $signature) {
                ob_clean();
                echo $_GET['echostr'];
            } else {
                echo 'xx';
            }
        } else {
            $this->responseMsg();
        }
    }

解决办法:在输出 echc $_GET['echostr']; 前,清空缓存区,即在echo 前放置“ob_clean();”代码即可。

官方解释:ob_clean(); 此函数是用来丢弃输出缓冲区中的内容;

完成以后,即可配置成功:

猜你喜欢

转载自www.cnblogs.com/Mishell/p/12618700.html