新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
PHP 微信公眾號的基本配置token驗證失敗

創(chuàng)新互聯(lián)專注于宏偉企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),電子商務(wù)商城網(wǎng)站建設(shè)。宏偉網(wǎng)站建設(shè)公司,為宏偉等地區(qū)提供建站服務(wù)。全流程按需定制,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
剛接觸微信公眾平臺開發(fā)的程序猿來說,一般都會遇到這樣的問題,在微信公眾號后臺中設(shè)置基本配置時,提示“token驗證失敗”,其實微信官方也給出了“接入指南”,但是文章真的太長了,相信很多人都沒耐心看完,小編也是其中一個,所以就直接網(wǎng)上解決方案,下面跟大家分享一下,驗證代碼如下(這個文件就是你填寫回調(diào)地址的目標(biāo)文件):
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 (!emptyempty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "%s0";
if(!emptyempty( $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, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
} 分享文章:PHP 微信公眾號的基本配置token驗證失敗
文章起源:http://m.5511xx.com/article/cocigoo.html


咨詢
建站咨詢
