<track id="f1xuz"><ruby id="f1xuz"><tt id="f1xuz"></tt></ruby></track>
<p id="f1xuz"></p>
  • 0712-2888027 189-8648-0214
    微信公眾號

    孝感風信網絡科技有限公司微信公眾號

    當前位置:主頁 > 技術支持 > PHP > 微信公眾號token驗證php代碼

    微信公眾號token驗證php代碼

    時間:2023-10-03來源:風信官網 點擊: 1119次
    微信公眾號開發過程中,都是使用自有服務器,所以我們需要對現有公眾號進行配置,才能將公眾號中的消息轉發到自有服務器上,但很多時候,我們在公眾號平臺服務器配置已經填寫正確的信息,提交確認信息的時候,總是提示:token驗證失敗,很是惱火,借助以下php代碼,可快速驗證服務器令牌(Token)的有效性。
    微信公眾號token驗證php代碼

    微信公眾號token驗證php代碼如下:

    <?php
    /**
     * wechat php test
     * update time: 20141008
     */
    //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 (!emptyempty($postStr)){
                $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>0</FuncFlag>
                </xml>";
                if(!emptyempty( $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, SORT_STRING);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
            if( $tmpStr == $signature ){
                return true;
            }else{
                return false;
            }
        }
    }
    ?>
    欄目列表
    推薦內容
    熱點內容
    展開
    美女自慰网站,亚洲一级a√无码,亚洲精品高清线久久,A大片免费久久精品