Easywechat中Token认证失败

第一步微信公众号配置时,token认证总是失败,发现easywechat中get微信传入的echostr时,得到的值一直是null,请问大家有没有遇到过类似的情况

JellyBool

额。。你具体的配置是什么?这样完全不知道你哪里有问题

1.你的微信开发者那里的配置
2.你的服务器的配置
3.你具体的路由,控制器的代码等

YiYang

sorry, 我一开始在你的视频底下发表评论,结果没通过审核貌似。
1.开发者和服务器里面的关于wechat_appid, token等都是一致的. laravel用的是最新版本,服务器部署是参照社区里的教程进行的。

  1. 路由只有两个,一个主页,一个是
Route::any('/wechat','WechatController@serve');

Controller中的代码:

class WechatController extends Controller
{
    //
    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        Log::info('request arrived.');
        $wechat = app('wechat');
        $wechat->server->setMessageHandler(function($message){
            return "欢迎关注 overtrue!";
        });
        Log::info('return response.');
        return $wechat->server->serve();
    }
}

我查看了easywechat中的Guard.php的源码,结合了storage/logs中的laravel.logwechat.log
其中laravel.log中报错的信息是

BadRequestException in Guard.php line 296: Invalid request.

以下是注释部分我的一些看法,还望指正

public function serve()
    {
        Log::debug('Request received:', [
            'Method' => $this->request->getMethod(),
            'URI' => $this->request->getRequestUri(),
            'Query' => $this->request->getQueryString(),
            'Protocal' => $this->request->server->get('SERVER_PROTOCOL'),
            'Content' => $this->request->getContent(),
        ]);
         //// 在token认证的时候下面这段if里面的内容并没有被执行,我也输出了 $this->request->get('echostr')的结果,日志里显示为null

        if ($str = $this->request->get('echostr')) {            
             Log::debug("Output 'echostr' is '$str'.");
            return new Response($str);
        }
      //下面这段代码反而被执行,在wechat.log中getContent的结果为null,所以handleRequest()方法抛出了异常invalid request
        $result = $this->handleRequest();

        $response = $this->buildResponse($result['to'], $result['from'], $result['response']);

        Log::debug('Server response created:', compact('response'));

        return new Response($response);
    }
YiYang

问题解决了,我的nginx配置错了。sorry

dxywx

@YiYang 请问具体哪里配置错了?

李奇聪

@YiYang 我的问题也是一样的 nginx配置能否粘贴一下?

YiYang

@dxywx 我的nginx少了一个问号,等下粘贴配置

YiYang

@dxywx
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /var/www/alittleone/public;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name 47.90.4.118;

location / {
	# First attempt to serve request as file, then
	# as directory, then fall back to displaying a 404.
	try_files $uri $uri/ /index.php?$query_string; //这里$query_string前面少了个问号
	# Uncomment to enable naxsi on this location
	# include /etc/nginx/naxsi.rules
}

这个是我的配置

YiYang

@李奇聪 我贴出来了

带泪的风小妖

我是Apache 也报这个错误为啥?

JellyBool 回复 带泪的风小妖

确保你的 apache 的重写规则符合 laravel 的要求基本就没有问题

带泪的风小妖 回复 JellyBool

应该是没有问题的!

JellyBool 回复 带泪的风小妖

apache 貌似还要配置 public 下的 .htaccess 吧。你去查一下怎么配置,默认的貌似不太可行

xhh1100

laravel的路由可以不使用中间件么,我新建routes/wechat.php

protected function mapWechatRoutes()
    {
        Route::group([
            'namespace' => 'App\Http\Controllers\Wechat',
            'prefix' => 'wechat',
            'middleware' => ''
        ], function ($router) {
            require base_path('routes/wechat.php');
        });
    }

为啥我这样,使用get,会不能访问呢