早睡早起,方能养生
Sleep early rise early, way to keep healthy

微信服务器验证TOKEN,报错$_GET['echostr']未定义数组索引

super
2020-08-07 21:09
views 3369

环境:

ThinkPHP5.1,Linux,nginx

 

问题:

微信公众号验证TOKEN,报错$_GET['echostr']未定义

 

解决:

ThinkPHP,把

$_GET['echostr'] => input('echostr');
$_GET['signature'] => input('signature');
$_GET['timestamp'] => input('timestamp');
$_GET['nonce'] => input('nonce');

 

以下(验证TOKEN)代码可直接使用(myToken改为你的TOKEN):

public function index()
{
    define("TOKEN", "myToken");
    $this->valid();
    exit();
}

public function valid()
{
    $echoStr = input("echostr");
 
    //valid signature , option
 
    if ($this->checkSignature()) {
        echo $echoStr;
        exit;
    }
}
 
private function checkSignature()
{
    $signature = input("signature");
    $timestamp = input("timestamp");
    $nonce = input("nonce");

    $token = TOKEN;

    $tmpArr = array($token, $timestamp, $nonce);
 
    sort($tmpArr);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );

    if ( $tmpStr == $signature ) {
        return true;
    } else {
        return false;
    }
}


分享
0 条讨论
top