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
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
> 因為組件默認使用 `Curl`,所以我們需要修改對應的 `GuzzleClient` 為協程客户端,或者修改常量 `SWOOLE_HOOK_FLAGS` 為 `SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL`
|
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
|
|
|
|
|
2019-12-12 16:24:04 +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 Hyperf\Guzzle\HandlerStackFactory;
|
|
|
|
|
use Overtrue\Socialite\Providers\AbstractProvider;
|
|
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
|
|
|
|
|
|
|
|
$container = ApplicationContext::getContainer();
|
|
|
|
|
|
2019-09-26 15:14:35 +08:00
|
|
|
|
$app = Factory::miniProgram($config);
|
2019-09-26 15:21:25 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
// 設置 HttpClient,當前設置沒有實際效果,在數據請求時會被 guzzle_handler 覆蓋,但不保證 EasyWeChat 後面會修改這裏。
|
2019-09-27 19:04:58 +08:00
|
|
|
|
$config = $app['config']->get('http', []);
|
|
|
|
|
$config['handler'] = $container->get(HandlerStackFactory::class)->create();
|
|
|
|
|
$app->rebind('http_client', new Client($config));
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
// 重寫 Handler
|
2019-09-27 19:04:58 +08:00
|
|
|
|
$app['guzzle_handler'] = new CoroutineHandler();
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
// 設置 OAuth 授權的 Guzzle 配置
|
2019-09-26 15:21:25 +08:00
|
|
|
|
AbstractProvider::setGuzzleOptions([
|
|
|
|
|
'http_errors' => false,
|
|
|
|
|
'handler' => HandlerStack::create(new CoroutineHandler()),
|
|
|
|
|
]);
|
2019-09-26 15:14:35 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 修改 `SWOOLE_HOOK_FLAGS`
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
修改入口文件 `bin/hyperf.php`,以下忽略不需要修改的代碼。
|
2019-09-26 15:14:35 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
2019-10-25 18:03:42 +08:00
|
|
|
|
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL);
|
2019-09-29 14:13:14 +08:00
|
|
|
|
```
|
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
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
|
|
$get = $this->request->getQueryParams();
|
|
|
|
|
$post = $this->request->getParsedBody();
|
|
|
|
|
$cookie = $this->request->getCookieParams();
|
|
|
|
|
$files = $this->request->getUploadedFiles();
|
|
|
|
|
$server = $this->request->getServerParams();
|
|
|
|
|
$xml = $this->request->getBody()->getContents();
|
|
|
|
|
|
|
|
|
|
$app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
|
|
return $response->getBody()->getContents();
|
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;
|
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);
|
|
|
|
|
```
|