小程序-奇葩的问题(Token校验失败,请检查确认)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013474104/article/details/78748671

网上借个图

陈科肇

按微信要求:确认此次GET请求来自微信服务器,则原样返回echostr参数内容

都按要求来了,但怎么都不行,总是提示:Token校验失败,请检查确认

仔细检查了配置好几次,都没发现能有什么问题

下面代码是官方检验signature的PHP示例代码:

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;
    }
}

这段代码与要求只差一步,那就是这段代码返回true或false,而不是GET请求中的参数echostr

我自己都通过浏览器手动输入参数认证了好几遍,都能验证成功,而且返回了echostr参数内容,那怎么微信开通设置老是提示Token校验失败,请检查确认

这时,我注意到一个问题,那就是return回来的echostr是带双引号,而echo回来的echostr是不带双引号的。

上图:
return
陈科肇

        $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) {
            if(isset($_GET['echostr'])){
                return $_GET['echostr'];
            }
        } else {
            wp_die('非法请求!!!');
        }

echo
陈科肇

        $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) {
            if(isset($_GET['echostr'])){
                echo $_GET['echostr'];
            }
        } else {
            wp_die('非法请求!!!');
        }

看这截图,不是感觉怪怪的,总感觉什么不对?

没错,返回的内容,虽然没双引号了,但多了个null字符串

因为我使用的是Wordpress 的REST API风格,会不会是因为这个问题?
然后自己试下了下,重新写个test.php

<?php
/**
 * Created by PhpStorm.
 * User: aaron
 * Date: 2017/12/8
 * Time: 9:48
 */


global $wpdb, $ft_tables;
$token = 'token536182947';//微信-客服消息TOKEN(令牌)

$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

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

if ($tmpStr == $signature) {
    echo $_GET['echostr'];
} else {
    echo 'xx';
}

手动加参数,浏览器测试,ok,果然是和我猜想的一样

陈科肇

好了,服务器准备完毕!!!

接下来再测试下提交“消息推送”!!!

陈科肇

擦。。。竟然真成功了!!!是不是很奇葩,还有这种操作

猜你喜欢

转载自blog.csdn.net/u013474104/article/details/78748671