Rumah > Artikel > pembangunan bahagian belakang > 服务器-【微信开发】为什么TOKEN一直验证失败呢?
服务器php微信开发
我是用的SAE新浪云来搞的,也进行了实名认证,接受数据的URL也没输错,但是就是token验证不通过,眼睛都看花了,麻烦能人帮忙找找错~谢谢!
<code> <?phpheader ('content-type:text/html;charset=utf-8'); //定义常量TOKEN,用来存储token define("TOKEN","weixin"); //封装验证逻辑 function checkSignature(){ //获取微信服务器发送的GET请求,得到四个参数 $signature = $_GET['signature']; $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; //定义数组,存储三个参数 $tmpArr = array(TOKEN,$nonce,$timestamp); //对数组进行排序 sort($tmpArr,SORT_STRING); //转换为字符串 $tempStr = implode($tmpArr); //进行sha1加密 $tempStr = sha1($tempStr); if($tempStr == $signature){ return true; }else{ return false; } } //判断是否验证成功 if(checkSignature()){ if($echostr){ $echostr = $_GET['echostr']; echo $echostr; exit(); } } //获取微信服务器发送的POST数据 $postData = $HTTP_RAW_POST_DATA; if(!$postData){ echo "error"; exit(); } //解析获得的数据 $object = simplexml_load_string($postData,"SimpleXMLElement",LIBXML_NOCDATA); $ToUserName = $object -> ToUserName; $FromUserName = $object -> FromUserName; $MsgType = $object -> MsgType; //根据不同类型的消息做出不同的回复 switch($MsgType){ case "text": $Content = $object -> Content; $respXml = "<xml> <tousername></tousername> <fromusername></fromusername> <createtime>&s</createtime> <msgtype></msgtype> <content></content> </xml>"; $result = sprintf($respXml,$FromUserName,$ToUserName,time(),$Content); echo $result; break; case "image": $MediaId = $object -> MediaId; $respXml = "<xml> <tousername></tousername> <fromusername></fromusername> <createtime>%s</createtime> <msgtype></msgtype> <image> <mediaid></mediaid> </image> </xml>"; $result = sprintf($respXml,$FromUserName,$ToUserName,time(),$MediaId); echo $result; break; }</code>