微信小程序消息模板推送 PHP
2019-11-05 本文已影响0人
QinHaolei
小程序相关: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html
用到的PHP框架: PhalApi
/** 第1步 获取access_token */
$curl = new \PhalApi\CUrl();
$rsToken = $curl->get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc5eef04cc5c8c4e0&secret=4add653bf0ff592d875125f57c3dd2df', 3000);
$rsToken = json_decode($rsToken, true);
/** 第2步 发送模板消息 */
try {
$curl = new \PhalApi\CUrl(2);
$curl->setHeader(array(
'Content-Type' => 'applecation/json'
));
$tontzhiPost = json_encode(array(
'touser' => '小程序openid',
'template_id' => '模板消息ID',
'form_id' => '用户提交表单时返回的formId',
'page' => '点击消息卡片 所要打开的小程序路径 pages/personal/order/index',
'data' => array(
'keyword1' => array(
"value" => '',
)
)
), JSON_UNESCAPED_SLASHES);
$rs = $curl->post(
'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $rsToken['access_token'],
$tontzhiPost,
3000
);
\PhalApi\DI()->logger->info('消息通知', $rs);
} catch (\PhalApi\Exception\InternalServerErrorException $ex) {
// 错误处理……
\PhalApi\DI()->logger->error('消息通知异常', $ex);
}