您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/12/30 23:35:50发布16次查看
代码直接用微信官方的例子来做小修改的
出现的问题是,部分消息验证不通过
例如我发送了一条信息,如果验证通过,回复欢迎信息,如果验证不通过,回复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 = %s 0 ; 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; } }}
回复讨论(解决方案) 记录日志,把获取合法返回的数据都记到日志里,看看成功和失败的日志区别来分析吧
你的验证参数字典排序不对
sort($tmparr);改成sort($tmparr, sort_string);
补充一下:至于你有时成功,有时失败,是因为成功的数据正好是两种排序结果是一样的。
你的验证参数字典排序不对
sort($tmparr);改成sort($tmparr, sort_string);
成了,感谢。
验证方法直接复制微信官方的例子,我太相信他们了,坑啊
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product