Home >Backend Development >PHP Tutorial >来技术论坛请问大神了,关于微信接口开发的

来技术论坛请问大神了,关于微信接口开发的

WBOY
WBOYOriginal
2016-06-13 12:05:26765browse

来技术论坛请教大神了,关于微信接口开发的
代码直接用微信官方的例子来做小修改的
出现的问题是,部分消息验证不通过
例如我发送了一条信息,如果验证通过,回复欢迎信息,如果验证不通过,回复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 />


------解决方案--------------------
记录日志,把获取合法返回的数据都记到日志里,看看成功和失败的日志区别来分析吧
------解决方案--------------------
你的验证参数字典排序不对
sort($tmpArr);改成sort($tmpArr, SORT_STRING);
------解决方案--------------------
补充一下:至于你有时成功,有时失败,是因为成功的数据正好是两种排序结果是一样的。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn