easy-swoole / wechat

Wechat based on easyswoole.
82 stars 27 forks source link

# easyswoole-wechat

说明

依赖环境

安装

$ composer require easyswoole/wechat 2.x --no-dev

基本使用

以服务端为例


    $config = [
        'appId' => 'wxefe41fdeexxxxxx',
        'token' => 'dczmnau31ea9nzcnxxxxxxxxx',
        'aesKey' => 'easyswoole'
    ];

    $officialAccount = \EasySwoole\WeChat\Factory::officialAccount($config);

    $server = $officialAccount->server;

    /** 注册消息事件回调 */
    $server->push(function (\EasySwoole\WeChat\Kernel\Contracts\MessageInterface $message) {
        return new \EasySwoole\WeChat\Kernel\Messages\Text(implode(",", $message->transformForJsonRequest()));
    });

    /** @var \Psr\Http\Message\ServerRequestInterface $psr7Request  */
    $psr7Request = mockRequest();

    /**
    * @var \Psr\Http\Message\ResponseInterface $reply
    * forceValidate() 表示启用请求验证,以确保请求来自微信发送。默认不启用验证
    * serve() 会解析本次请求后回调之前注册的事件(包括AES解密和解析XML)
    * server() 接受一个显式实现了 \Psr\Http\Message\ServerRequestInterface 的request对象
    */
    $reply = $server->forceValidate()->serve($psr7Request);

    /**
    * $reply 是一个显式实现了PSR-7的对象,用户只需要处理该对象即可正确响应给微信
    * 下面是一个原生swoole的响应方法
    */
    $swooleResponse->status($reply->getStatusCode());

    /**
     * PSR-7 的Header并不是单纯的k => v 结构
     */
    foreach ($reply->getHeaders() as $name => $values) {
        $swooleResponse->header($name, implode(", ", $values));
    }

    $swooleResponse->write($reply->getBody()->__toString());

以客户端为例


    $config = [
        'appId' => 'wxefe41fdeexxxxxx',
        'token' => 'dczmnau31ea9nzcnxxxxxxxxx',
        'aesKey' => 'easyswoole'
    ];

    $officialAccount = \EasySwoole\WeChat\Factory::officialAccount($config);

    /** 获取用户列表 */
    $list = $officialAccount->user->list();