2019-09-26 15:14:35 +08:00
|
|
|
|
# EasyWechat
|
|
|
|
|
|
2019-09-29 14:13:14 +08:00
|
|
|
|
[EasyWeChat](https://www.easywechat.com/) 是一个开源的微信 SDK (非微信官方 SDK)。
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
2021-01-12 16:38:37 +08:00
|
|
|
|
> 因为组件默认使用 `Curl`,所以我们需要修改对应的 `GuzzleClient` 为协程客户端,或者修改常量 [SWOOLE_HOOK_FLAGS](/zh-cn/coroutine?id=swoole-runtime-hook-level)
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
|
|
|
|
## 替换 `Handler`
|
|
|
|
|
|
2020-08-09 18:16:51 +08:00
|
|
|
|
以下以公众号为例,
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
2019-09-27 19:04:58 +08:00
|
|
|
|
use Hyperf\Utils\ApplicationContext;
|
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
|
use EasyWeChat\Kernel\ServiceContainer;
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
|
use Hyperf\Guzzle\CoroutineHandler;
|
|
|
|
|
use Overtrue\Socialite\Providers\AbstractProvider;
|
|
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
|
|
|
|
|
|
|
|
$container = ApplicationContext::getContainer();
|
|
|
|
|
|
2020-08-09 18:14:37 +08:00
|
|
|
|
$app = Factory::officialAccount($config);
|
|
|
|
|
$handler = new CoroutineHandler();
|
2019-09-26 15:21:25 +08:00
|
|
|
|
|
2020-08-09 18:14:37 +08:00
|
|
|
|
// 设置 HttpClient,部分接口直接使用了 http_client。
|
2019-09-27 19:04:58 +08:00
|
|
|
|
$config = $app['config']->get('http', []);
|
2020-08-09 18:14:37 +08:00
|
|
|
|
$config['handler'] = $stack = HandlerStack::create($handler);
|
2019-09-27 19:04:58 +08:00
|
|
|
|
$app->rebind('http_client', new Client($config));
|
|
|
|
|
|
2020-08-09 18:14:37 +08:00
|
|
|
|
// 部分接口在请求数据时,会根据 guzzle_handler 重置 Handler
|
|
|
|
|
$app['guzzle_handler'] = $handler;
|
2019-09-27 19:04:58 +08:00
|
|
|
|
|
2020-08-09 18:14:37 +08:00
|
|
|
|
// 如果使用的是 OfficialAccount,则还需要设置以下参数
|
|
|
|
|
$app->oauth->setGuzzleOptions([
|
2019-09-26 15:21:25 +08:00
|
|
|
|
'http_errors' => false,
|
2020-08-09 18:14:37 +08:00
|
|
|
|
'handler' => $stack,
|
2019-09-26 15:21:25 +08:00
|
|
|
|
]);
|
2019-09-26 15:14:35 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 修改 `SWOOLE_HOOK_FLAGS`
|
|
|
|
|
|
2021-01-12 16:38:37 +08:00
|
|
|
|
参考 [SWOOLE_HOOK_FLAGS](/zh-cn/coroutine?id=swoole-runtime-hook-level)
|
2019-10-26 16:03:37 +08:00
|
|
|
|
|
|
|
|
|
## 如何使用 EasyWeChat
|
|
|
|
|
|
|
|
|
|
`EasyWeChat` 是为 `PHP-FPM` 架构设计的,所以在某些地方需要修改下才能在 Hyperf 下使用。下面我们以支付回调为例进行讲解。
|
|
|
|
|
|
|
|
|
|
1. `EasyWeChat` 中自带了 `XML` 解析,所以我们获取到原始 `XML` 即可。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$xml = $this->request->getBody()->getContents();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2. 将 XML 数据放到 `EasyWeChat` 的 `Request` 中。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
2020-08-09 18:26:49 +08:00
|
|
|
|
use Symfony\Component\HttpFoundation\HeaderBag;
|
2019-10-26 16:03:37 +08:00
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
|
|
$get = $this->request->getQueryParams();
|
|
|
|
|
$post = $this->request->getParsedBody();
|
|
|
|
|
$cookie = $this->request->getCookieParams();
|
2020-07-23 18:16:06 +08:00
|
|
|
|
$uploadFiles = $this->request->getUploadedFiles() ?? [];
|
2019-10-26 16:03:37 +08:00
|
|
|
|
$server = $this->request->getServerParams();
|
|
|
|
|
$xml = $this->request->getBody()->getContents();
|
2020-07-23 18:16:06 +08:00
|
|
|
|
$files = [];
|
|
|
|
|
/** @var \Hyperf\HttpMessage\Upload\UploadedFile $v */
|
|
|
|
|
foreach ($uploadFiles as $k => $v) {
|
|
|
|
|
$files[$k] = $v->toArray();
|
|
|
|
|
}
|
2020-08-09 18:26:49 +08:00
|
|
|
|
$request = new Request($get, $post, [], $cookie, $files, $server, $xml);
|
|
|
|
|
$request->headers = new HeaderBag($this->request->getHeaders());
|
2020-08-12 21:44:43 +08:00
|
|
|
|
$app->rebind('request', $request);
|
2019-10-26 16:03:37 +08:00
|
|
|
|
// Do something...
|
2020-05-21 10:38:26 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
3. 服务器配置
|
|
|
|
|
|
|
|
|
|
如果需要使用微信公众平台的服务器配置功能,可以使用以下代码。
|
|
|
|
|
|
|
|
|
|
> 以下 `$response` 为 `Symfony\Component\HttpFoundation\Response` 并非 `Hyperf\HttpMessage\Server\Response`
|
|
|
|
|
> 所以只需将 `Body` 内容直接返回,即可通过微信验证。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$response = $app->server->serve();
|
|
|
|
|
|
2020-07-24 09:34:50 +08:00
|
|
|
|
return $response->getContent();
|
2019-10-26 16:03:37 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 如何替换缓存
|
|
|
|
|
|
|
|
|
|
`EasyWeChat` 默认使用 `文件缓存`,而现实场景是 `Redis` 缓存居多,所以这里可以替换成 `Hyperf` 提供的 `hyperf/cache` 缓存组件,如您当前没有安装该组件,请执行 `composer require hyperf/cache` 引入,使用示例如下:
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
2019-11-16 19:12:33 +08:00
|
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2019-10-26 16:03:37 +08:00
|
|
|
|
use Hyperf\Utils\ApplicationContext;
|
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
|
|
|
|
|
|
$app = Factory::miniProgram([]);
|
|
|
|
|
$app['cache'] = ApplicationContext::getContainer()->get(CacheInterface::class);
|
|
|
|
|
```
|