{"id":239,"title":"\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\u5f00\u53d1\u4e00\u4e2a\u5165\u95e8\u4f8b\u5b50","good":0,"bad":0,"hit":2179,"created_at":"2016-05-31 01:10:15","content":"

<?php\/**  * wechat php test  *\/\/\/define your tokendefine("TOKEN", "wwwgupiaojzcom");$wechatObj = new wechatCallbackapiTest();\/\/$wechatObj->valid();\/\/\u5f00\u59cb\u5f00\u53d1\u7684\u65f6\u5019\u8981\u628a\u8fd9\u53e5\u6ce8\u91ca\u6389\uff0c\u6389\u7528\u4e0b\u9762\u7684responseMsg();<\/strong>$wechatObj->responseMsg();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)){                \/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,                   the best way is to check the validity of xml by yourself *\/                libxml_disable_entity_loader(true);                  $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(!empty( $keyword ))                {                    if($keyword==99){                        $contentStr="\u67e5\u5b5899\u5143\u5347\u503c\uff0c\u683c\u5f0f\uff1a\u7537\u58eb\u59d3\u540d+\u5973\u58eb\u59d3\u540d\uff0c\u5982\uff1a\u5c0f\u660e+\u5c0f\u7ea2";                        $msgType = "text";<\/strong>                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                        echo $resultStr;                    }else {                        $contentStr= "\u67e5\u5b5899\u5143\u5347\u503c\uff0c\u683c\u5f0f\uff1a\u7537\u58eb\u59d3\u540d+\u5973\u58eb\u59d3\u540d\uff0cn\u5982\uff1a\u5c0f\u660e+\u5c0f\u7ea2";                        $contentStr.= "\u516c\u4f17\u53f7ibelove";                        $msgType = "text";<\/strong>                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                        echo $resultStr;                    }                }        }else {            echo "";            exit;        }    }            private function checkSignature()    {        \/\/ you must define TOKEN by yourself        if (!defined("TOKEN")) {            throw new Exception('TOKEN is not defined!');        }                $signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];                        $token = TOKEN;        $tmpArr = array($token, $timestamp, $nonce);        \/\/ use SORT_STRING rule        sort($tmpArr, SORT_STRING);        $tmpStr = implode( $tmpArr );        $tmpStr = sha1( $tmpStr );                if( $tmpStr == $signature ){            return true;        }else{            return false;        }    }}?><\/p>"}