Update wechat.md (#790)

This commit is contained in:
李铭昕 2019-10-26 16:03:37 +08:00 committed by GitHub
parent 0d10dda04d
commit d6e66f87af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,3 +51,46 @@ AbstractProvider::setGuzzleOptions([
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL);
```
## 如何使用 EasyWeChat
`EasyWeChat` 是为 `PHP-FPM` 架构设计的,所以在某些地方需要修改下才能在 Hyperf 下使用。下面我们以支付回调为例进行讲解。
1. `EasyWeChat` 中自带了 `XML` 解析,所以我们获取到原始 `XML` 即可。
```php
$xml = $this->request->getBody()->getContents();
```
2. 将 XML 数据放到 `EasyWeChat``Request` 中。
```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...
```
## 如何替换缓存
`EasyWeChat` 默认使用 `文件缓存`,而现实场景是 `Redis` 缓存居多,所以这里可以替换成 `Hyperf` 提供的 `hyperf/cache` 缓存组件,如您当前没有安装该组件,请执行 `composer require hyperf/cache` 引入,使用示例如下:
```php
<?php
use Hyperf\Cache\CacheInterface;
use Hyperf\Utils\ApplicationContext;
use EasyWeChat\Factory;
$app = Factory::miniProgram([]);
$app['cache'] = ApplicationContext::getContainer()->get(CacheInterface::class);
```