微信のキーワード返信テキスト情報


(1)検証token(2)token検証が通過した後、キーワード返信情報のソースコードは以下の通りである.

/**
  * wechat php test
  */

//define your token
define("TOKEN", "sayhello");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();//        !
$wechatObj->responseTextMsg();//      
class wechatCallbackapiTest
{  

    public function valid()
    {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
      //      
    public function responseTextMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //  POST xml  
        if (!empty($postStr)){

                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "
                            
                            
                            %s
                            
                            
                            0
                            ";  


                if(!empty( $keyword )&&strstr($keyword, "   "))
                { 

                    $msgType = "text";
                    $contentStr = "      ";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;


                }else{
                    echo "Input something...";
                }

        }else {
            echo "123";
            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;
        }
    }
}

?>