微信公众号订阅消息|小程序模板消息

function CurlPOST($url, $data = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output, true);
return $output;

}

公众号消息接口: 

https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . self::getWxToken()

example:
$template_id = 'XXXXXXXXXXXX';
$replace_arr['data'] = [
'first' => [
'value' => '你的供稿人认证未通过审核,请修改认证信息后重新提交申请',
'color' => '#1E1E1E',
],
'keyword1' => [
'value' => '供稿人认证',
'color' => '#404040',
],

'remark' => [
'value' => 'XXXXXXXX',
'color' => '#828282',
],
];
$replace_arr['touser'] = $touser;
$replace_arr['template_id'] = $template_id;
$replace_arr['url'] = 'https://shiquaner.com';
//预发布和正式采用跳转链接
//if (Util_Conf::getEnv() != 'test') {
// $replace_arr['miniprogram'] = [
// 'appid' => Util_Conf::get('XIAO_WECHAT_APP_ID'),
// 'pagepath' => $jump_url,
// ];
//}
$data = json_encode($replace_arr, JSON_UNESCAPED_UNICODE);
$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . self::getWxToken();
Util_Log::write(1, '=====发送公众号消息=====' . $url);
$result = self::CurlPOST($url, $data);
Util_Log::write(1, '=====发送公众号消息结束=====' . json_encode($result));

小程序发送订阅消息:
https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;

 

 example:

$check_msg =  [
'thing1' => [
'value' => '一起过牛年',
],
'thing2' => [
'value' => '排行榜,将在3天后计票截止',
],
'thing3' => [
'value' => '排行榜前18名均可获得奖项,赶快加油!',
],
'thing4' => [
'value' => '特奖名额多,可优先集满21牛气值',
],
];
$template_id = 'zUnp1rJwGcoc1BHSMzN5C9v3zq1XXjpLyK89oo56MGY';
$res = [
'template_id' => $template_id,
'data' => $check_msg,
];
$send_arr =  $res; 
$send_arr['touser'] = $touser;
$res = self::curl_post($url,json_encode($send_arr));
$res = json_decode($res,true);
return $res;
 
 
 
posted @ 2022-03-10 17:33  龙卷风之殇  阅读(627)  评论(0)    收藏  举报