php版微信开发Token验证失败或请求URL超时问题的解决方法
本文实例分析了php版微信开发Token验证失败或请求URL超时问题的解决方法。分享给大家供大家参考,具体如下:
微信开发最近要用到的一个功能,其实就是一个非常的简单的用户输入然后自动搜索数据库并进行一个数据回复了,这个与官方没多大的问题,但小编就微信Token验证失败折腾了许多,下面解决了给各位分析一下.
1.Token验证失败
这个就是要检查配置文件了,最基本的就是
define("TOKEN","weixin"); weixin是你的微信开发后台的ID
微信开发Token验证失败或请求URL超时问题解决办法
2.请求URL超时
这个没什么办法多提交几次了,这个还有就是服务器安装了安全狗之类的软件把微信IP给拦截了,可以检查一下。
3.官方下载一个wechatCallbackapiTest类然后进行一下操作即可,代码如下:
define("TOKEN","weixin");
$wechatObj=newwechatCallbackapiTest();
if(isset($_GET['echostr'])){
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
wechatCallbackapiTest类就代码如下:
classwechatCallbackapiTest
{
publicfunctionvalid()
{
$echoStr=$_GET["echostr"];
if($this->checkSignature()){
echo$echoStr;
exit;
}
}
privatefunctioncheckSignature()
{
$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){
returntrue;
}else{
returnfalse;
}
}
publicfunctionresponseMsg()
{
$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-dH: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程序设计有所帮助。