p> $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $買粉絲ntentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
買粉絲公眾平臺里面token驗證這個要多嘗試幾次,因為可能有延遲,并不是實時的,多嘗試幾次
但是要確認你的代碼是真的沒有問題,并且地址填寫的沒有問題
用得著判斷那個TOKEN?是不是多此一舉,用我這個,包你滿意
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends WechatController {
public function index(){
$v = $_GET["echostr"];
if(!isset($v)){ //根據隨機字符串判斷是否之前已經驗證過,避免重復驗證
$this->receive();
}else{
$this->checkSignature();
}
}
//未驗證執行驗證
public function checkSignature(){
$signature = I("signature");//加密簽名
$timestamp = I("timestamp");//時間戳
$nonce = I("nonce");//隨機數
$token = TOKEN;//token
$tmpArr = array($token,$timestamp, $nonce);//組成新數組
sort($tmpArr,SORT_STRING);//重新排序
$tmpStr = implode($tmpArr);//轉換成字符串
$tmpStr = sha1($tmpStr);//再將字符串進行加密
if( $tmpStr == $signature )
{
echo $_GET["echostr"];
exit;
//return true;
}else{
return false;
}
}
//已經驗證過執行這里
public function receive(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//獲取用戶發送的消息,格式為xml
//$postStr = file_get_買粉絲ntents("php://input");
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, "SimpleXMLElement", LIBXML_NOCDATA);
$this->logger($postStr);
//并且回復信息給用戶
if(!empty($postObj)){
switch($postObj->MsgType){
case "text&qu
2024-07-23 04:43
2024-07-23 04:21
2024-07-23 04:06
2024-07-23 03:32
2024-07-23 03:17
2024-07-23 02:40