后端开发
网络新概念,云计算、大数据、O2O、电商。。。。
网络新概念,云计算、大数据、O2O、电商。。。。
2017-07-27 19:46:22
1、概要:
网站和系统经常会发送一些通知提醒,而微信公众号模版消息是一个比较好的解决方案,一方面提醒到了,另一方面还可以提高客户的粘度;
2、设计模型:
在开发的时候使用通知消息队列方式实现,消息队列相当于一个消息中心,所有需要发送消息提醒的系统或商城通过api接口将消息内容推送到消息中心,然后微信模版消息发送只需要对接消息中心,增加了很多灵活度,比如可以对没有绑定微信的用户发送短信提醒,可以对发送的频率进行总体控制等等。

微信公众号发送模版消息PHP实现代码
消息队列
3、PHP实现主要代码:
模版消息文档很简单,需要登录微信公众平台后在模版消息边上查看到
微信公众号发送模版消息PHP实现代码
$data = array(
'first' => array(
'value'=> '您好,售后机已经收到!',
'color' => '#743A3A',
),
'keyword1' => array('value' => '2017-07-24 10:00',
'color' => '#743A3A',
),
'keyword2' => array('value' => 'XX公司大库',
'color' => '#743A3A',
),
'Remark' => array('value' => '你的订单已提交,我们尽快发货',
'color' => '#743A3A',
),
);
$touser='onLThw0j5cyMx_n0n8ZyPydU5HJY';
$template_id='WfE6_NVYTvKCmNzR2qRnYf6J7e2mTnX7FR82zaXKHJY';
$furl='http://weixin.qq.com/';
$topcolor = '#7B68EE';
$array = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $furl,
'topcolor'=>'#FF0000',
'data' => $data,
);
$access_token = _get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$return = $this->send_post( $url, urldecode(json_encode($array)));
echo '返回值:' . $return;模版消息文档
4、php发送微信模版消息post请求函数
使用curl方式一直不成功,使用下面的方式发送微信公众号模版消息成功
/**发送微信模板消息成功*/
function send_post($url, $post_data ) {
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/json',
//header 需要设置为 JSON
'content' => $post_data,
'timeout' => 60
//超时时间
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return $result;
}