微信模板消息推送,这个只在测试号上做,如果需要,请按照各自的情况,做成你需要的样子。
做一个demo吧,
在测试号中,申请一个模板消息,获取到模板ID,
为:
您的{{project.DATA}}项目,本期已结清<br> 项目名称:{{project.DATA}} 到款金额:{{received_payments.DATA}} 到款时间:{{time.DATA}} 收益金额:{{income.DATA}}
$model_id = "你获取的模板id";
写一个mould.php
class mould_weixin{public $access_token; public function __construct($acccess_token){ $this->access_token = $acccess_token; } //发送模板消息 public function send_template_message($data){ $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token; $res = $this->http_request($url, $data); return json_decode($res, true); } //https请求(支持GET和POST) protected function http_request($url, $data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } }
写一个发送模板消息的文件,send.php
记得,先获取access_token,才能成功。
在该文件中,引入model.php
$mould = new mould_weixin($access_token);//调用发送模板消息 //项目状态提醒 $openid = $_POST[‘openid‘];//用户的openid $project = $_POST[‘project‘];//项目名称 $limit = $_POST[‘limit‘];//本期额度 $time = $_POST[‘time‘];//上线时间 $income = $_POST[‘income‘];//年化收益 $type = $_POST[‘type‘];//还款方式 $model_id = "IenwDpxSJFrtVd9TnTt4n4UxXojAFeY9orrRXZ9mTAo";//项目状态模板ID $template = array(‘touser‘ => "$openid", ‘template_id‘ => "$model_id", ‘url‘ => "http://www.rongtuojinrong.com/", ‘topcolor‘ => "#7B68EE",//顶部颜色,自定义 ‘data‘ => array(‘project‘ => array(‘value‘ => urlencode("project"), ‘color‘ => "#FF0000", ), ‘limit‘ => array(‘value‘=>urlencode("limit"), ‘color‘ => "#FF0000", ), ‘time‘=>array(‘value‘=>urlencode("time"), ‘color‘=>"#FF0000", ), ‘income‘=>array(‘value‘=>urlencode("income"), ‘color‘=>"#FF0000", ), ‘type‘=>array(‘value‘=>urlencode("type"), ‘color‘=>"#FF0000", ), ) );
var_dump($model->send_template_message(urldecode(json_encode($template))));