PHP code example of ella123 / hyperf-wechat
1. Go to this page and download the library: Download ella123/hyperf-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/ */
ella123 / hyperf-wechat example snippets
Router::addRoute(['GET', 'POST', 'HEAD'], '/wechat', 'App\Controller\WeChatController@serve');
declare(strict_types=1);
namespace App\Controller;
use EasyWeChat\Kernel\Exceptions\BadRequestException;use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;use EasyWeChat\Kernel\Exceptions\InvalidConfigException;use Ella123\HyperfWechat\EasyWechat;use ReflectionException;
class WeChatController extends AbstractController
{
/**
* 处理微信的请求消息
*
* @return string
* @throws BadRequestException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws ReflectionException
*/
public function serve()
{
$app = EasyWechat::officialAccount();
// 监听事件
$app->getServer()->addEventListener();
// 监听消息
$app->getServer()->addMessageListener();
return $app->getServer()->serve();
}
}
use \Ella123\HyperfWechat\EasyWechat;
$officialAccount = EasyWechat::officialAccount(); // 公众号
$work = EasyWechat::work(); // 企业微信
$pay = EasyWechat::pay(); // 微信支付
$openPlatform = EasyWechat::openPlatform(); // 开放平台
$miniApp = EasyWechat::miniApp(); // 小程序
// 均支持传入配置账号名称以及配置
EasyWeChat::officialAccount('foo',[]); // `foo` 为配置文件中的名称,默认为 `default`。`[]` 可覆盖账号配置
//...
shell script
php ./bin/hyperf.php vendor:publish ella123/hyperf-wechat