微信公共号给客户发送消息提醒/模板消息(如业务到期提醒 ,订单提醒,帐户变动提醒)

首先要开通模板消息接口权限,然后添加消息模板,保存模板id

以业务到期提醒为例:

第一步:获取access_token;

参数appid,appsecret

获取方法:

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

  $ret_json = file_get_contents($url);
      
       $ret = json_decode($ret_json);

$ret->access_token

define('ACCESS_TOKEN',$ret->access_token);

编辑模板;

  $data=array('touser'=>OPENID,   //发给谁
    'template_id'=>'9dZByMs3VnvYqdgJUoluAYofFkO9pdBS7beXS_vortI',   //模板id
    'url'=>'http://doing.demenk.com/dxshop/mobile',     //这个是你发送了模板消息之后,当用户点击时跳转的连接
    'topcolor'=>"#FF0000",   //颜色
    'data'=>array(
    'first'=>array(
    'value'=>'中国电信',
    'color'=>'#173177'
    ),
    'type'=>array(
    'value'=>'宽带到期提醒',
    'color'=>'#173177'
    ),
    'date'=>array(
    'value'=>2018-01-20,
    'color'=>'#173177'
    ),
    'remark'=>array(
    'value'=>'请尽快办理续费业务,以免因断网给你带来不便',
    'color'=>'#173177'
    )
    )
    );

发送POST请求:

$data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".ACCESS_TOKEN);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
    return curl_error($ch);
}
curl_close($ch);
print_r($tmpInfo);   //输出返回结果为  {"errcode":0,"errmsg":"ok","msgid":113183143401455617}

微信公共号给客户发送消息提醒/模板消息(如业务到期提醒 ,订单提醒,帐户变动提醒)