imaxwen / yii2-easy-wechat

WeChat SDK for yii2 , based on overtrue/wechat.
MIT License
188 stars 57 forks source link

yii2下无法正常工作 #8

Closed wblbird closed 7 years ago

wblbird commented 7 years ago

1.独立安装easywechat,正常运行,用微信发送信息,公众号能正常自动回复消息。说明我服务器环境没问题。

2.先安装了基础版本yii2,composer用的是国外源 composer create-project --prefer-dist yiisoft/yii2-app-basic basic cd basic composer require maxwen/yii2-easy-wechat 经过配置,以下代码,服务器能正确验证,但是公众号不能正常回复消息, 显示“该公众号暂时无法提供服务“ $app = Yii::$app->wechat; $server = $app->server; $server->setMessageHandler(function ($message) { return "您好!欢迎关注我!"; }); $response = $server->serve(); $response->send();

微信上发送信息给公众号,微信不能正常回复消息,log无debug信息。

log在验证服务器配置的时候是有debug信息的,如下: [2017-06-08 22:50:16] easywechat.DEBUG: Current config: {"debug":true,"app_id":"bd809","secret":"8610b","token":"qyzg111","aes_key":"23s1aIgUiYJKcts3zAf1yCFkQvULV49zKFMpGirhljq","log":{"level":"debug","permission":511,"file":"/home/wwwroot/wx.***.com/log.txt"}} [] [2017-06-08 22:50:16] easywechat.DEBUG: Request received: {"Method":"GET","URI":"/site/weixin?signature=fec0b467a15bf7ea7075d10a8e413a242cb375b4&echostr=1389749417337909086&timestamp=1496933464&nonce=3075139355","Query":"echostr=1389749417337909086&nonce=3075139355&signature=fec0b467a15bf7ea7075d10a8e413a242cb375b4&timestamp=1496933464","Protocal":"HTTP/1.0","Content":""} [] [2017-06-08 22:50:16] easywechat.DEBUG: Output 'echostr' is '1389749417337909086'. [] []

大概哪里出了问题呢??

imaxwen commented 7 years ago

@wblbird 微信官方后台里有没有开启服务器配置?或者配置是否正确?

wblbird commented 7 years ago

我非常仔细 看了easywechat的文档, 1.已经开启服务器配置了(后台已经显示红色“停用”按钮) 2.配置应该正确的吧,因为因为服务器验证的时候是成功的,且debug的log有信息记录的(上面已经罗列了内容)

配置文件如下: params.php

<?php

return [
   'WECHAT' => [
        'debug'  => true,

        'app_id'  => 'wx03236a*****d809',         // AppID
        'secret'  => '69ee2b2173*******610b',     // AppSecret
        'token'   => 'qyzg111',          // Token
        'aes_key' => '23s1a*********rhljq',                    // EncodingAESKey,安全模式下请一定要填写!!!
        'log' => [
            'level' => 'debug',
            'permission' => 0777,
            'file'  => '/home/wwwroot/wx.xxxx.com/log.txt', // XXX: 绝对路径!!!!
        ],
    ],

    'adminEmail' => 'admin@example.com',
];

控制器代码

public function actionWeixin(){
        $app = Yii::$app->wechat;
        $server = $app->server;
        $server->setMessageHandler(function ($message) {
            return "您好!欢迎关注我!";
        });
        $response = $server->serve();
        $response->send();
}

web.php代码

'components' => [
            'wechat' => [
            'class' => 'maxwen\easywechat\Wechat',
            // 'userOptions' => []  # user identity class params
            // 'sessionParam' => '' # wechat user info will be stored in session under this key
            // 'returnUrlParam' => '' # returnUrl param stored in session
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => [
                // ...
            ],
        ],
.......
imaxwen commented 7 years ago

@wblbird

两个建议可以尝试一下:

  1. controller中使用 return $response->send();
  2. 如果还是不行 去微信后台 -- 运维中心 -- 日志查询 查找相应的日志找到原因。
wblbird commented 7 years ago

搞定了,通过 微信后台 -- 运维中心 -- 日志查询 发现原来是yii2的Csrf的问题,关闭它就OK了。 搞了这么久,忘记这茬了。。

谢谢您耐心的答复!