php查询mysql增加模板消息_微信小程序&PHP 发送模板消息通知

注意

模板消息接口需写在后台服务器上。【追加:目前微信小程序只能给本人反馈】

模板消息模板需要在小程序后台申请配置!

2018.4.9修改 :

1次提交表单可下发1条,多次提交下发条数独立,相互不影响;

1次支付可下发3条,多次支付下发条数独立,互相不影响。

小程序端

根据模板信息,出到后台相应的值,还有formid或者prepay_id,通知人的openid。

.wxml文件

之前忘记加上来了,因为必须是formid或者prepay_id。所以得有一个按钮去触发取得。

如果大家有不用手动触发的方法,还请不吝赐教!!!!这个问题想了很久!!

提现 //为防止多次提交设置lock

.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)

},

})

}

后台接口 发送模板消息至 微信用户

_message.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 = array(

'touser' => $o_id,

'template_id' => $template_id,

'form_id' => $formid,

'data' => array(

"keyword1" => array("value"=>$u_name),

"keyword2" => array( "value"=>$money ),

"keyword3" => array("value" => $to_place),

"keyword4" => array( "value" => $dated),

"keyword5" => array("value" => $remark),

)

);

$data = json_encode($data);

$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; // 返回数据

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值