这篇文章主要介绍了关于微信小程序和php 发送模板消息通知,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
首先,模板消息接口需写在后台服务器上。【追加!目前微信小程序 模板消息只能 本人触发 并发给本人。】
2018.4.9修改 :1次提交表单可下发1条,多次提交下发条数独立,相互不影响;
1次支付可下发3条,多次支付下发条数独立,互相不影响。
小程序端
根据模板信息,出到后台相应的值,还有formid或者prepay_id,通知人的openid。
.wxml文件
之前忘记加上来了,因为必须是formid或者prepay_id。所以得有一个按钮去触发取得。如果大家有不用手动触发的方法,还请不吝赐教!!!!这个问题想了很久!!
<form bindsubmit="tixian" report-submit='true'>
<button class='btn' form-type="submit" disabled="{{lock}}">提现</button>
</form>
.js文件
/**
* 触发微信提醒
*/
remindmessage: function (formid){
var that = this
wx.request({
method: 'post',
url: 'https://www.**********************_message.php',//后台接口
data: {
o_id: that.data.allthing.openid,
u_name: that.data.allthing.userinfo.nickname,
money: that.data.money,
formid: formid
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
console.log(res.data)
},
})
}
后台接口
*****.php
<?php
include_once('/o**********************c/function.php');
include_once('/o*************************on/config.php');
$ms = new mysqls();
$o_id = intval($_post['o_id']);
$u_name = addslashes($_post['u_name']);
$money = floatval($_post['money']);
$formid = addslashes($_post['formid']);
$dated = date("y-m-d h:i:s");
$to_place = "微信钱包";
$remark = "余额约 0-5 个工作日返回您的充值账户,请等待余额到账通知";
$template_id = "zaw*******************4dy"; //模板id
$access_token=m::get('q******_'.$appid);//需要使用token。放在缓存中!!
if(!$access_token){
$url_access_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$json_access_token = sendcmd($url_access_token,array());
//access_token加缓存
$arr_access_token = json_decode($json_access_token,true);
$access_token = $arr_access_token['access_token'];
m::set('qub*************_'.$appid,$access_token,3600);
}
if(!$o_id || !$u_name || !$money || !$formid){
$arr = array(
'ret'=>0,
'msg'=>'参数错误!'
);
echo json_encode($arr);
die();
}
if(!empty($access_token)) {
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token; //此处变量插入字符串不能使用{}!!!
$data = '{
"touser":"'.$o_id.'",
"template_id":"'.$template_id.'",
"form_id":"'.$formid.'",
"data": {
"keyword1": {
"value":"'.$u_name.'"
},
"keyword2": {
"value":"'.$money.'"
},
"keyword3": {
"value":"'.$to_place.'"
} ,
"keyword4": {
"value":"'.$dated.'"
} ,
"keyword5": {
"value":"'.$remark.'"
}
}
}';
$result = sendcmd($url,$data);
$arr = array('ret'=>1,
'msg'=>'success',
'data'=>array('result'=>$result),
);
} else {
$arr = array('ret'=>0,'msg'=>'access token为空!');
}
echo json_encode($arr);
/**
* 发起请求
* @param string $url 请求地址
* @param string $data 请求数据包
* @return string 请求返回数据
*/
function sendcmd($url,$data)
{
$curl = curl_init(); // 启动一个curl会话
curl_setopt($curl, curlopt_url, $url); // 要访问的地址
curl_setopt($curl, curlopt_ssl_verifypeer, 0); // 对认证证书来源的检测
curl_setopt($curl, curlopt_ssl_verifyhost, 2); // 从证书中检查ssl加密算法是否存在
curl_setopt($curl, curlopt_httpheader, array('expect:')); //解决数据包大不能提交
curl_setopt($curl, curlopt_followlocation, 1); // 使用自动跳转
curl_setopt($curl, curlopt_autoreferer, 1); // 自动设置referer
curl_setopt($curl, curlopt_post, 1); // 发送一个常规的post请求
curl_setopt($curl, curlopt_postfields, $data); // post提交的数据包
curl_setopt($curl, curlopt_timeout, 30); // 设置超时限制防止死循
curl_setopt($curl, curlopt_header, 0); // 显示返回的header区域内容
curl_setopt($curl, curlopt_returntransfer, 1); // 获取的信息以文件流的形式返回
$tmpinfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'errno'.curl_error($curl);
}
curl_close($curl); // 关键curl会话
return $tmpinfo; // 返回数据
}
?>
相关推荐:
详解php发送邮件知识点
php发送短信邮件等众多实用php代码分享
以上就是微信小程序和php 发送模板消息通知的详细内容。