根据获取到的 access_token、模板 ID 和用户的 openID 构造模板消息,通过微信接口将模板消息发送给用户

<?php
// 构造模板消息数据
$template_data = array(
    'touser' => '用户的openID',
    'template_id' => '模板消息ID',
    'data' => array(
        'first' => array(
            'value' => '您好,您有新的消息!',
            'color' => '#173177'
        ),
        'keyword1' => array(
            'value' => '消息标题',
            'color' => '#173177'
        ),
        'keyword2' => array(
            'value' => '消息内容',
            'color' => '#173177'
        ),
        'remark' => array(
            'value' => '请及时处理。',
            'color' => '#173177'
        )
    )
);

// 获取 access_token
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的AppID&secret=你的AppSecret';
$response = file_get_contents($url);
$access_token = json_decode($response, true)['access_token'];

// 发送模板消息
$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
$options = array(
    'http' => array(
        'header' => 'Content-type: application/json',
        'method' => 'POST',
        'content' => json_encode($template_data)
    )
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
echo $response;
?>

其中,需要替换的部分有:

  • 用户的openID:替换成要发送消息的用户的 openID。
  • 模板消息ID:替换成在公众号或小程序后台设置好的模板消息的 ID。
  • 你的AppID 和 你的AppSecret:替换成在微信开放平台上创建的应用的 AppID 和 AppSecret。

这里使用了 PHP 的 file_get_contents 函数来发送 HTTP 请求,并使用了 json_decode 函数来处理数据。根据实际情况修改代码中的参数即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值