2019-09-26 15:14:35 +08:00
|
|
|
|
# EasyWechat
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
[EasyWeChat](https://www.easywechat.com/) 是一個開源的微信 SDK (非微信官方 SDK)。
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
2022-12-27 13:37:04 +08:00
|
|
|
|
> 如果您使用了 Swoole 4.7.0 及以上版本,並且開啓了 native curl 選項,則可以不按照此文檔進行操作。
|
2021-08-30 10:46:59 +08:00
|
|
|
|
|
2021-09-09 09:29:08 +08:00
|
|
|
|
> 因為組件默認使用 `Curl`,所以我們需要修改對應的 `GuzzleClient` 為協程客户端,或者修改常量 [SWOOLE_HOOK_FLAGS](/zh-hk/coroutine?id=swoole-runtime-hook-level)
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
## 替換 `Handler`
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
2020-08-10 13:28:28 +08:00
|
|
|
|
以下以公眾號為例,
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
2023-04-11 15:57:32 +08:00
|
|
|
|
use Hyperf\Context\ApplicationContext;
|
2019-09-27 19:04:58 +08:00
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
|
use Hyperf\Guzzle\CoroutineHandler;
|
|
|
|
|
|
|
|
|
|
$container = ApplicationContext::getContainer();
|
|
|
|
|
|
2020-08-10 13:28:28 +08:00
|
|
|
|
$app = Factory::officialAccount($config);
|
|
|
|
|
$handler = new CoroutineHandler();
|
2019-09-26 15:21:25 +08:00
|
|
|
|
|
2020-08-10 13:28:28 +08:00
|
|
|
|
// 設置 HttpClient,部分接口直接使用了 http_client。
|
2019-09-27 19:04:58 +08:00
|
|
|
|
$config = $app['config']->get('http', []);
|
2020-08-10 13:28:28 +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-10 13:28:28 +08:00
|
|
|
|
// 部分接口在請求數據時,會根據 guzzle_handler 重置 Handler
|
|
|
|
|
$app['guzzle_handler'] = $handler;
|
2019-09-27 19:04:58 +08:00
|
|
|
|
|
2020-08-10 13:28:28 +08:00
|
|
|
|
// 如果使用的是 OfficialAccount,則還需要設置以下參數
|
|
|
|
|
$app->oauth->setGuzzleOptions([
|
2019-09-26 15:21:25 +08:00
|
|
|
|
'http_errors' => false,
|
2020-08-10 13:28:28 +08:00
|
|
|
|
'handler' => $stack,
|
2019-09-26 15:21:25 +08:00
|
|
|
|
]);
|
2019-09-26 15:14:35 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 修改 `SWOOLE_HOOK_FLAGS`
|
|
|
|
|
|
2021-09-09 09:29:08 +08:00
|
|
|
|
參考 [SWOOLE_HOOK_FLAGS](/zh-hk/coroutine?id=swoole-runtime-hook-level)
|
2019-10-26 16:03:37 +08:00
|
|
|
|
|
|
|
|
|
## 如何使用 EasyWeChat
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
`EasyWeChat` 是為 `PHP-FPM` 架構設計的,所以在某些地方需要修改下才能在 Hyperf 下使用。下面我們以支付回調為例進行講解。
|
2019-10-26 16:03:37 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
1. `EasyWeChat` 中自帶了 `XML` 解析,所以我們獲取到原始 `XML` 即可。
|
2019-10-26 16:03:37 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$xml = $this->request->getBody()->getContents();
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
2. 將 XML 數據放到 `EasyWeChat` 的 `Request` 中。
|
2019-10-26 16:03:37 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
2020-08-10 13:28:28 +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-27 15:52:51 +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-27 15:52:51 +08:00
|
|
|
|
$files = [];
|
|
|
|
|
/** @var \Hyperf\HttpMessage\Upload\UploadedFile $v */
|
|
|
|
|
foreach ($uploadFiles as $k => $v) {
|
|
|
|
|
$files[$k] = $v->toArray();
|
|
|
|
|
}
|
2020-08-10 13:28:28 +08:00
|
|
|
|
$request = new Request($get, $post, [], $cookie, $files, $server, $xml);
|
|
|
|
|
$request->headers = new HeaderBag($this->request->getHeaders());
|
2020-08-31 13:40:33 +08:00
|
|
|
|
$app->rebind('request', $request);
|
2019-10-26 16:03:37 +08:00
|
|
|
|
// Do something...
|
2020-05-21 14:47:20 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
3. 服務器配置
|
|
|
|
|
|
|
|
|
|
如果需要使用微信公眾平台的服務器配置功能,可以使用以下代碼。
|
|
|
|
|
|
|
|
|
|
> 以下 `$response` 為 `Symfony\Component\HttpFoundation\Response` 並非 `Hyperf\HttpMessage\Server\Response`
|
|
|
|
|
> 所以只需將 `Body` 內容直接返回,即可通過微信驗證。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$response = $app->server->serve();
|
|
|
|
|
|
2020-07-27 15:52:51 +08:00
|
|
|
|
return $response->getContent();
|
2019-10-26 16:03:37 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
## 如何替換緩存
|
2019-10-26 16:03:37 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
`EasyWeChat` 默認使用 `文件緩存`,而現實場景是 `Redis` 緩存居多,所以這裏可以替換成 `Hyperf` 提供的 `hyperf/cache` 緩存組件,如您當前沒有安裝該組件,請執行 `composer require hyperf/cache` 引入,使用示例如下:
|
2019-10-26 16:03:37 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
2019-11-16 19:12:33 +08:00
|
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2023-04-11 15:57:32 +08:00
|
|
|
|
use Hyperf\Context\ApplicationContext;
|
2019-10-26 16:03:37 +08:00
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
|
|
|
|
|
|
$app = Factory::miniProgram([]);
|
|
|
|
|
$app['cache'] = ApplicationContext::getContainer()->get(CacheInterface::class);
|
|
|
|
|
```
|