小程序消息推送配置,Token校验失败,请检查确认?
发布于 4 年前 作者 leitian 9170 次浏览 来自 问答

url网站根目录下的文件:test/WechatCallbackapi.php,代码如下:

<?php

/**

  * wechat php test

  */

//define your token

define(“TOKEN”, “weixin”);

$wechatObj = new wechatCallbackapiTest();

$wechatObj->valid();

class wechatCallbackapiTest

{

public function valid()

    {

        $echoStr = $_GET[“echostr”];

        //valid signature , option

        if($this->checkSignature()){

        echo $echoStr;

        exit;

        }

    }

    public function responseMsg()

    {

	//get post data, May be due to the different environments

	$postStr = $GLOBALS\["HTTP\_RAW\_POST\_DATA"\];

      //extract post data

	if (!empty($postStr)){

 

              $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement’, LIBXML_NOCDATA);

                $fromUsername = $postObj->FromUserName;

                $toUsername = $postObj->ToUserName;

                $keyword = trim($postObj->Content);

                $time = time();

                $textTpl = "<xml>

						&lt;ToUserName&gt;&lt;!\[CDATA\[%s\]\]&gt;&lt;/ToUserName&gt;

						&lt;FromUserName&gt;&lt;!\[CDATA\[%s\]\]&gt;&lt;/FromUserName&gt;

						&lt;CreateTime&gt;%s&lt;/CreateTime&gt;

						&lt;MsgType&gt;&lt;!\[CDATA\[%s\]\]&gt;&lt;/MsgType&gt;

						&lt;Content&gt;&lt;!\[CDATA\[%s\]\]&gt;&lt;/Content&gt;

						&lt;FuncFlag&gt;0&lt;/FuncFlag&gt;

						&lt;/xml&gt;";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

			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()

{

        $signature = $_GET[“signature”];

        $timestamp = $_GET[“timestamp”];

        $nonce = $_GET[“nonce”];

 

	$token = TOKEN;

	$tmpArr = array($token, $timestamp, $nonce);

	sort($tmpArr);

	$tmpStr = implode( $tmpArr );

	$tmpStr = sha1( $tmpStr );

 

	if( $tmpStr == $signature ){

		return true;

	}else{

		return false;

	}

}

}

?>

回到顶部