PHP code example of qinii / wechat-official-account
1. Go to this page and download the library: Download qinii/wechat-official-account 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/ */
qinii / wechat-official-account example snippets
use Qinii\WechatCore\Enum\ApplicationType;
use Qinii\WechatOfficialAccount\OfficialAccount;
$wechat = OfficialAccount::create([
'default' => [
ApplicationType::OFFICIAL_ACCOUNT => [
'app_id' => 'wx1234567890',
'appsecret' => 'your-app-secret',
'token' => 'your-callback-token',
'aes_key' => 'your-encoding-aes-key',
'use_stable_access_token' => true,
'oauth' => [
'redirect_url' => 'https://example.com/wechat/oauth',
'scopes' => ['snsapi_userinfo'],
],
'http' => [
'timeout' => 5,
],
],
],
]);
use Qinii\WechatCore\Config\ConfigResolver;
use Qinii\WechatOfficialAccount\OfficialAccount;
$wechat = new OfficialAccount(
configResolver: $configResolver,
accountName: 'default',
);
// 切换账号,不修改原门面
$other = $wechat->account('other');
// 注入 PSR-16 缓存,生产环境建议使用 Redis 等共享缓存
$wechat->setCache($cache);
// 注入 Symfony HttpClientInterface 实例
$wechat->setHttpClient($httpClient);
// 获取底层 EasyWeChat 应用,调用尚未封装的接口
$application = $wechat->application();
use Qinii\WechatCore\Http\SwooleHttpClient;
$wechat->setHttpClient(new SwooleHttpClient([
'http_version' => '1.1',
'timeout' => 5,
]));
$url = $wechat->oauth()->redirect(
redirectUrl: 'https://example.com/wechat/oauth',
scopes: ['snsapi_userinfo'],
state: 'your-state',
);
$token = $wechat->oauth()->tokenFromCode($code);
$user = $wechat->oauth()->userFromCode($code);
$user = $wechat->oauth()->userFromToken($accessToken, $openid);
$user = $wechat->user()->get($openid);
$config = $wechat->jsSdk()->build(
url: $url,
apiList: ['updateAppMessageShareData', 'chooseWXPay'],
);
$wechat->menu()->create([
[
'type' => 'view',
'name' => '商城',
'url' => 'https://example.com',
],
]);
$temporary = $wechat->qrCode()->temporary('login:123', 600);
$forever = $wechat->qrCode()->forever('spread:123');
$imageUrl = $wechat->qrCode()->url($forever['ticket']);
$image = $wechat->material()->uploadImage('/absolute/path/image.jpg');
$voice = $wechat->material()->uploadVoice('/absolute/path/voice.mp3');
$result = $wechat->template()->send(
openId: $openid,
templateId: $templateId,
data: [
'first' => '订单支付成功',
'amount' => ['99.00 元', '#ff0000'],
'remark' => ['value' => '感谢您的购买'],
],
url: 'https://example.com/orders/123',
);
$wechat->template()->setIndustry(1, 4);
$templates = $wechat->template()->getPrivateTemplates();
$result = $wechat->template()->addTemplate(
'TM00015',
['订单编号', '订单金额', '订单状态'],
);
$wechat->template()->deletePrivateTemplate($templateId);
$industry = $wechat->template()->getIndustry();
$wechat->customerService()->sendText($openid, '您好');
$wechat->customerService()->sendImage($openid, $mediaId);
$wechat->customerService()->sendVoice($openid, $mediaId);
$wechat->customerService()->sendNews($openid, [
[
'title' => '订单提醒',
'description' => '您有一条新订单消息',
'url' => 'https://example.com/orders',
'image' => 'https://example.com/order.jpg',
],
]);
$message = $wechat->message();
$response = $wechat->server()
->onEvent('subscribe', function ($incoming) use ($message) {
return $message->text('感谢关注');
})
->onMessage('text', function ($incoming) use ($message) {
return $message->text('收到:' . $incoming->Content);
})
->serve($request);
$message->text('文本');
$message->image($mediaId);
$message->voice($mediaId);
$message->video($mediaId, '标题', '说明');
$message->news($articles);
$message->transfer('account@wechat');