docs/en/sdks/wechat.md
#EasyWechat
EasyWeChat is an open source WeChat SDK (not WeChat official SDK).
If you are using Swoole 4.7.0 and above, and have the native curl option turned on, you may not follow this document.
Because the component uses
Curlby default, we need to modify the correspondingGuzzleClientas a coroutine client, or modify the constant SWOOLE_HOOK_FLAGS
HandlerThe following takes the public account as an example,
<?php
use Hyperf\Context\ApplicationContext;
use EasyWeChat\Factory;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Guzzle\CoroutineHandler;
$container = ApplicationContext::getContainer();
$app = Factory::officialAccount($config);
$handler = new CoroutineHandler();
// Set HttpClient, some interfaces use http_client directly.
$config = $app['config']->get('http', []);
$config['handler'] = $stack = HandlerStack::create($handler);
$app->rebind('http_client', new Client($config));
// Some interfaces will reset the Handler according to guzzle_handler when requesting data
$app['guzzle_handler'] = $handler;
// If you are using OfficialAccount, you also need to set the following parameters
$app->oauth->setGuzzleOptions([
'http_errors' => false,
'handler' => $stack,
]);
SWOOLE_HOOK_FLAGSReference SWOOLE_HOOK_FLAGS
EasyWeChat is designed for PHP-FPM architecture, so it needs to be modified in some places to be used under Hyperf. Let's take the payment callback as an example to explain.
EasyWeChat comes with XML parsing, so we can get the original XML.$xml = $this->request->getBody()->getContents();
Request of EasyWeChat.<?php
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request;
$get = $this->request->getQueryParams();
$post = $this->request->getParsedBody();
$cookie = $this->request->getCookieParams();
$uploadFiles = $this->request->getUploadedFiles() ?? [];
$server = $this->request->getServerParams();
$xml = $this->request->getBody()->getContents();
$files = [];
/** @var \Hyperf\HttpMessage\Upload\UploadedFile $v */
foreach ($uploadFiles as $k => $v) {
$files[$k] = $v->toArray();
}
$request = new Request($get, $post, [], $cookie, $files, $server, $xml);
$request->headers = new HeaderBag($this->request->getHeaders());
$app->rebind('request', $request);
// Do something...
If you need to use the server configuration function of the WeChat public platform, you can use the following code.
The following
$responseisSymfony\Component\HttpFoundation\ResponsenotHyperf\HttpMessage\Server\ResponseSo just return theBodycontent directly to pass WeChat verification.
$response = $app->server->serve();
return $response->getContent();
EasyWeChat uses file cache by default, but the actual scenario is that Redis cache is mostly used, so this can be replaced with the hyperf/cache cache component provided by Hyperf, if you do not currently install this component, please execute composer Introduced by require hyperf/cache, the usage example is as follows:
<?php
use Psr\SimpleCache\CacheInterface;
use Hyperf\Context\ApplicationContext;
use EasyWeChat\Factory;
$app = Factory::miniProgram([]);
$app['cache'] = ApplicationContext::getContainer()->get(CacheInterface::class);