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

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

WBOY
Release: 2016-06-23 13:51:07
Original
889 people have browsed it

代码直接用微信官方的例子来做小修改的
出现的问题是,部分消息验证不通过
例如我发送了一条信息,如果验证通过,回复欢迎信息,如果验证不通过,回复false关键词
我发了5条信息,有时候有两条回复false,有时候有三条false。
为什么会有验证不通过的现象?
求大神指导

define("TOKEN", "token");$wechatObj = new wechatCallbackapiTest();//$wechatObj->valid();$wechatObj->run();class wechatCallbackapiTest{	public function valid()    {        $echoStr = $_GET["echostr"];        //valid signature , option        if($this->checkSignature()){        	echo $echoStr;        	exit;        }    }        public function run() {    	if($this->checkSignature()) {    		$this->responseMsg();    	}else{    		$this->responseMsg("false");    	}    }    public function responseMsg($contentStr = "Welcome to wechat world!")    {		//get post data, May be due to the different environments		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];      	//extract post data		if (!empty($postStr)){                              	$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 ))                {              		$msgType = "text";                	//$contentStr = "Welcome to wechat world!";                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                	echo $resultStr;                }else{                	echo "Input something...";                }        }else {        	echo "";        	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;		}	}}
Copy after login


回复讨论(解决方案)

记录日志,把获取合法返回的数据都记到日志里,看看成功和失败的日志区别来分析吧

你的验证参数字典排序不对
sort($tmpArr);改成sort($tmpArr, SORT_STRING);

补充一下:至于你有时成功,有时失败,是因为成功的数据正好是两种排序结果是一样的。

你的验证参数字典排序不对
sort($tmpArr);改成sort($tmpArr, SORT_STRING);



成了,感谢。
验证方法直接复制微信官方的例子,我太相信他们了,坑啊
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template