PHP发送微信小程序模板消息通知

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34827048/article/details/79562996

这里写图片描述

  • formId 或 prepay_id:
用户必须得提交了表单或进行了支付才能推送模板消息,
表单提交后能得到 formId,
支付完成能得到 prepay_id,
而且一个 formId 或 prepay_id 只能推送一条消息。
  • openID:
推送给谁的用户标识符
  • template_id:

模板消息的模板编号,小程序的后台申请
  • access_token:

access_token 是全局唯一接口调用凭据,开发者调用各接口时都需使用 access_token,
access_token 的有效期目前为2个小时,
需定时刷新,重复获取将导致上次获取的 access_token 失效。
  • 后台
/**
 * SendTemplate 触发模板通知
 *
 * @return reposne
 */
public function SendTemplate($openId,$formId,$product,$activity)
{
    $tempalte_id = 'lv9T-PcgWn-Rkhq-1MxwaxvotO2VU3prc-wk1';
    $date_time = date('Y-m-d h:i:s', time());
    $data=array(
            'keyword1'  => array('value'=>$activity,'color'=>'#000000'),
            'keyword2'  => array('value'=>$product,'color'=>'#000000'),
            'keyword3'  => array('value'=>$date_time,'color'=>'#000000'),
            'keyword4'  => array('value'=>'15901419475','color'=>'#000000'),
    );

    $template = array(
        'touser' => $openId,
        'template_id' => $tempalte_id,
        'url' => 'pages/index/index',
        'form_id'=>$formId,
        'topcolor' =>'#7B68EE',
        'data' => $data,
    );

    $appid  =  "*****"
    $secret =   "*****
    $url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";

    $access_token=$this->httpGet($url);

    $access_token=JSON_decode($access_token)->access_token;

    $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token";

    $template =  json_encode($template);

    $result = $this->httpPost($url,$template,'json');

    return $result;

}
  • 官方文档
http://t.cn/RmvjgtF

猜你喜欢

转载自blog.csdn.net/qq_34827048/article/details/79562996