本文实例分析了php版微信开发token验证失败或请求url超时问题的解决方法。分享给大家供大家参考,具体如下:
微信开发最近要用到的一个功能,其实就是一个非常的简单的用户输入然后自动搜索数据库并进行一个数据回复了,这个与官方没多大的问题,但小编就微信token验证失败折腾了许多,下面解决了给各位分析一下.
1.token验证失败
这个就是要检查配置文件了,最基本的就是
define("token", "weixin"); weixin 是你的微信开发后台的id
微信开发token验证失败或请求url超时问题解决办法
2.请求url超时
这个没什么办法多提交几次了,这个还有就是服务器安装了安全狗之类的软件把微信ip给拦截了,可以检查一下。
3.官方下载一个wechatcallbackapitest类然后进行一下操作即可,代码如下:
define("token", "weixin"); $wechatobj = new wechatcallbackapitest(); if (isset($_get['echostr'])) { $wechatobj->valid(); }else{ $wechatobj->responsemsg(); }
wechatcallbackapitest类就代码如下:
class wechatcallbackapitest { public function valid() { $echostr = $_get["echostr"]; if($this->checksignature()){ echo $echostr; 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; } } public function responsemsg() { $poststr = $globals["http_raw_post_data"]; if (!emptyempty($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($keyword != " " || !emptyempty( $keyword ) ) { msgtype = "text"; //$contentstr .= date("y-m-d h:i:s",time()); $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr); echo $resultstr; } }else{ echo ""; exit; } } }
更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
YII2框架中使用RBAC对模块,控制器,方法的权限控制及规则的使用示例
YII2框架中ActiveDataProvider与GridView的配合使用操作示例
PhpStorm的使用教程(本地运行PHP+远程开发+快捷键)
网友评论