有人遇到过 Swoft 接入 EasyWechat 扩展,微信验证服务端 token 失败问题吗?

我用的环境#

  • PHP 版本:7.2
  • overtrue/wechat 版本:4.1
  • Swoole 版本:4.4.3
  • Swoole 框架名称: Swoft2.0.5

问题及现象#

微信公众号监听用户关注事件,微信公众号后台需要验证服务器端 token
swoft 框架不能使用 exit , 文档中说 $response->send() 直接输出(echo)了,请问用 swoft 的方式怎么去做

WeChatController

$config=[
    ....
];
$app = Factory::officialAccount($config);
$response = $app->server->serve();
return \context()->getResponse()
->withContentType('text/xml')
->withContent($response->getContent());

有人使用过Swoft 接入EasyWechat扩展,服务端验证微信token失败问题吗

求助中

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案

已解决,主动初始化。后续有问题继续更新

$this->app = Factory::officialAccount(config('easyWeChat'));
$get = \context()->getRequest()->get() ?? [];
$post= \context()->getRequest()->post() ?? [];
$attr = [];
$cookies = \context()->getRequest()->cookie() ?? [];
$files = \context()->getRequest()->file() ?? [];
$server = \context()->getRequest()->server() ?? [];
$raw = \context()->getRequest()->raw() ?? [];
$this->app->request->initialize($get, $post, $attr, $cookies, $files, $server, $raw);
5年前 评论
讨论数量: 5

你现在的这个做法不行吗

5年前 评论
程序员祝融 (楼主) 5年前
jake666 (作者) 5年前

这个可能不太兼容,easywechat 比较适合传统的 fpm 模式框架,常驻内存的框架可能会有坑

5年前 评论
pardon110

Swoole 常驻内存,会将每次请求的生命周期扩展到整个 worker 进程。因此,你需要基于 swoole 事件在每次连接建立或关闭时,做一些初始化和清除的工作,Swoft 框架本身对已经有了一些方案。换句话来说,要让你的代码正常运行,需要编码让类似微信公众号监听用户这样的请求不常驻内存(即需要每次有新请求初始化,结束时清理)。而有些常驻是有益的,比如 redis,数据库连接之类。

5年前 评论

在 Swoft\Http\Message 里加上 :

public function getContent()
{
    return $this->raw();
}

public static function getContentType(){

}

public static function getSchemeAndHttpHost() {

}

然后响应那里:  
    $response111 = $this->app->server->serve();
    return $response->withContent($response111->getContent());
5年前 评论

已解决,主动初始化。后续有问题继续更新

$this->app = Factory::officialAccount(config('easyWeChat'));
$get = \context()->getRequest()->get() ?? [];
$post= \context()->getRequest()->post() ?? [];
$attr = [];
$cookies = \context()->getRequest()->cookie() ?? [];
$files = \context()->getRequest()->file() ?? [];
$server = \context()->getRequest()->server() ?? [];
$raw = \context()->getRequest()->raw() ?? [];
$this->app->request->initialize($get, $post, $attr, $cookies, $files, $server, $raw);
5年前 评论