Rumah > Artikel > pembangunan bahagian belakang > 来技术论坛请问大神了,关于微信接口开发的
来技术论坛请教大神了,关于微信接口开发的
代码直接用微信官方的例子来做小修改的
出现的问题是,部分消息验证不通过
例如我发送了一条信息,如果验证通过,回复欢迎信息,如果验证不通过,回复false关键词
我发了5条信息,有时候有两条回复false,有时候有三条false。
为什么会有验证不通过的现象?
求大神指导
<br />define("TOKEN", "token");<br />$wechatObj = new wechatCallbackapiTest();<br />//$wechatObj->valid();<br />$wechatObj->run();<br /><br />class wechatCallbackapiTest<br />{<br /> public function valid()<br /> {<br /> $echoStr = $_GET["echostr"];<br /><br /> //valid signature , option<br /> if($this->checkSignature()){<br /> echo $echoStr;<br /> exit;<br /> }<br /> }<br /> <br /> public function run() {<br /> if($this->checkSignature()) {<br /> $this->responseMsg();<br /> }else{<br /> $this->responseMsg("false");<br /> }<br /> }<br /><br /> public function responseMsg($contentStr = "Welcome to wechat world!")<br /> {<br /> //get post data, May be due to the different environments<br /> $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];<br /><br /> //extract post data<br /> if (!empty($postStr)){<br /> <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 = "Welcome to wechat world!";<br /> $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);<br /> echo $resultStr;<br /> }else{<br /> echo "Input something...";<br /> }<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 /> <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 />