Rumah > Artikel > pembangunan bahagian belakang > 第一个大众微信demo老是运行不成功
第一个公众微信demo老是运行不成功
我找了很多代码了,差不多都是如下代码:
<?php<br />define("TOKEN", "imates");//自己定义的token 就是个通信的私钥<br />$wechatObj = new wechatCallbackapiTest();<br />$wechatObj->valid();<br />$wechatObj->responseMsg();<br />class wechatCallbackapiTest<br />{<br /> public function valid()<br /> {<br /> $echoStr = $_GET["echostr"];<br /> if($this->checkSignature()){<br /> echo $echoStr;<br /> exit;<br /> }<br /> }<br /> public function responseMsg()<br /> {<br /> $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];<br /> if (!empty($postStr)){<br /> $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br /> $fromUsername = $postObj->FromUserName;<br /> $toUsername = $postObj->ToUserName;<br /> $keyword = trim($postObj->Content);<br /> $time = time();<br /> $textTpl = "<xml><br /> <ToUserName><![CDATA[%s]]></ToUserName><br /> <FromUserName><![CDATA[%s]]></FromUserName><br /> <CreateTime>%s</CreateTime><br /> <MsgType><![CDATA[%s]]></MsgType><br /> <Content><![CDATA[%s]]></Content><br /> <FuncFlag>0<FuncFlag><br /> </xml>";<br /> if(!empty( $keyword ))<br /> {<br /> $msgType = "text";<br /> $contentStr = '你好啊,屌丝';<br /> $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);<br /> echo $resultStr;<br /> }else{<br /> echo '咋不说哈呢';<br /> }<br /> }else {<br /> echo '咋不说哈呢';<br /> exit;<br /> }<br /> }<br /><br /> private function checkSignature()<br /> {<br /> $signature = $_GET["signature"];<br /> $timestamp = $_GET["timestamp"];<br /> $nonce = $_GET["nonce"];<br /> $token =TOKEN;<br /> $tmpArr = array($token, $timestamp, $nonce);<br /> sort($tmpArr);<br /> $tmpStr = implode( $tmpArr );<br /> $tmpStr = sha1( $tmpStr );<br /><br /> if( $tmpStr == $signature ){<br /> return true;<br /> }else{<br /> return false;<br /> }<br /> }<br />}<br />?>