PHP code example of vzina / wechat
1. Go to this page and download the library: Download vzina/wechat library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vzina / wechat example snippets
use EasyWeChat\Factory;
$options = [
'app_id' => 'wx3cf0f39249eb0exxx',
'secret' => 'f1c242f4f28f735d4687abb469072xxx',
'token' => 'easywechat',
'log' => [
'level' => 'debug',
'file' => '/tmp/easywechat.log',
],
// ...
];
$app = Factory::officialAccount($options);
$server = $app->server;
$user = $app->user;
$server->push(function($message) use ($user) {
$fromUser = $user->get($message['FromUserName']);
return "{$fromUser->nickname} 您好!欢迎关注 overtrue!";
});
$server->serve()->send();
declare (strict_types=1);
namespace Yef\WeChat;
use EasyWeChat\Kernel\Events\AccessTokenRefreshed;
use EasyWeChat\Kernel\Events\ApplicationInitialized;
use EasyWeChat\Kernel\Events\HttpResponseCreated;
use EasyWeChat\Kernel\Events\ServerGuardResponseCreated;
use Hyperf\Cache\CacheManager;
use Hyperf\Context\Context;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Arr;
use Hyperf\Utils\Str;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Class Factory
*
* @inheritDoc
*/
class Factory extends \EasyWeChat\Factory
{
/**
* @param string $name
* @return \EasyWeChat\Kernel\ServiceContainer
*/
public static function make($name, array $config = [])
{
$adapter = Str::snake((string)$name);
$contextKey = 'hy.wechat.' . ($config['app_id'] ?? $adapter);
return Context::getOrSet($contextKey, function () use ($adapter, $config) {
$container = ApplicationContext::getContainer();
$config = $config ?: (array)config('wechat.' . $adapter);
$prepends = [
// 设置hy缓存对象
'cache' => fn() => $container->get(CacheManager::class)
->getDriver($config['cache_name'] ?? 'wechat_redis_cache'),
// 设置hy请求对象
'request' => function () use ($container) {
$request = $container->get(ServerRequestInterface::class);
if (empty($request)) {
return Request::createFromGlobals();
}
return Request::create(
(string)$request->getUri(),
$request->getMethod(),
$request->getQueryParams(),
$request->getCookieParams(),
$request->getUploadedFiles(),
$request->getServerParams(),
(string)$request->getBody()->getContents()
);
},
];
// 设置hy监听事件
$listener = fn($event) => $container->get(EventDispatcherInterface::class)->dispatch($event);
Arr::set($config, 'events.listen', [
AccessTokenRefreshed::class => [$listener],
ApplicationInitialized::class => [$listener],
HttpResponseCreated::class => [$listener],
ServerGuardResponseCreated::class => [$listener],
]);
$namespace = Str::studly($adapter);
$application = "\\EasyWeChat\\{$namespace}\\Application";
$app = new $application($config, $prepends);
return $app;
});
}
public static function __callStatic($name, $arguments)
{
return static::make($name, ...$arguments);
}
}