php 微信公众号发送消息模板
2022-05-31PHP
模板消息的申请需要账号已经开通微信支付权限。在微信公众平台的后台,依次进入“功能->添加功能插件->模板消息”,即可申请模板消息。申请时,选择2个和自己相关的行业即可。提交并且申请通过后,可以在模板库中看到模板消息列表
示例:发送购买的消息模板
其他发送的消息模板与之类似
// 构建一个http发送 // $url 发送的url路径 // $data 发送的post数据 function http_request($url, $data = array()) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // post数据 curl_setopt($ch, CURLOPT_POST, 1); // 把post的变量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); curl_close($ch); return $output; } // 发送购买的消息模板 // $openid 发送用户的openid // $url 点击模板消息后跳转的链接 // $body 商品名称 // $cash_fee 商品价格,单位为:分 // $access_token access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。 // 公众号可以使用AppID和AppSecret调用本接口来获取access_token。 function yx_send_buy_template_message($openid,$url,$body,$cash_fee,$access_token){ $tpl = array( //接收者 这里面要用到用户的openid "touser"=>$openid, //这里是你要发送的模板类型 "template_id"=>"FC3ZOeSqnvHFk3jaKOV9AsExK9g5Siaqx1udQPNgTgI", //这里是点击模板跳转的页面 "url" => $url, //标题颜色 "topcolor" => "#FFAE89", "data" => array( //这里的first,keyword 都要对应这模板里面的信息写 "first" => array("value" => '你好,你已成功购买'.$body,"color" => "#000"), "keyword1" => array("value" => $body,"color" => "#0178c5"), "keyword2" => array("value" => $cash_fee,"color" => "#0178c5"), "keyword3" => array("value" => '支付成功',"color" => "#0178c5"), // 这里是通用的 就类似与备注的存在 "remark" => array("value" => "点击详情查看","color" => "#999"), ), ); $json_template = json_encode($tpl); $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token"; http_request($url,$json_template); }
很赞哦! ()