php网页微信登录验证失败,微信开发Token验证失败解决方法
本篇文章中講述了在微信開發中遇到Token驗證失敗的解決方法,對微信開發感興趣的或者遇到過token驗證失敗解決不了的,都可以看看本篇文章哦!廢話少說,我們來進入正題吧!
微信小程序配置消息推送的時候一般都會出現Token驗證失敗的問題,這個錯誤是因為,你的接口頁面還沒有反饋正確的信息給微信接口,網友們也給出了一些解決方法,但有些能夠配置成功,有些則不然。下面給出網友提供的2種比較容易配置成功的php接口驗證代碼。
代碼示例一(我的驗證可以成功):<?php
//1. 將timestamp , nonce , token 按照字典排序
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = "你自定義的Token值";
$signature = $_GET['signature'];
$array = array($timestamp,$nonce,$token);
sort($array);
//2.將排序后的三個參數拼接后用sha1加密
$tmpstr = implode('',$array);
$tmpstr = sha1($tmpstr);
//3. 將加密后的字符串與 signature 進行對比, 判斷該請求是否來自微信
if($tmpstr == $signature)
{
echo $_GET['echostr'];
exit;
}
代碼示例二:<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "自定義token");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//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;
}
}
}
?>
以上兩種實例代碼選擇一種直接上傳到你的服務器上,在消息配置中的url(服務器地址寫上php文件的服務器地址),填入對應的自定義Token(令牌),消息加密密鑰隨機生成即可,加密方式我的填了兼容模式,數據格式隨個人喜好吧(我的填了JSON)。然后直接點擊提交即可。如果出現下圖說明驗證通過了:
大家遇到過Token驗證錯誤的可以看看哦!幫助你更快的解決token的難題!
相關推薦:
總結
以上是生活随笔為你收集整理的php网页微信登录验证失败,微信开发Token验证失败解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: word中插入常用几种公式编辑器
- 下一篇: php ajax jquery瀑布流,j