微信小程序开发之发送模板消息

1,小程序wxml页面form表单添加 report-submit="true"

<form bindsubmit="sub" report-submit="true">
<button formType="submit">确认发布</button>
</form>

2,小程序js代码 (formId唯一且只有提交表单时产生,只能使用一次)

Page({
// 页面的初始数据
data: {},
sub: function (e) {
console.log(e.detail.formId); // 获取formId,发送至服务器端
}
});

3,php程序代码

// 发送get请求
function curlGet($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
if(curl_errno($curl)){
return 'ERROR ' . curl_error($curl);
}
curl_close($curl);
return $output;
} // 获取小程序用户access_token
function getToken(){
$appid = '小程序公众平台中的APPID'; // 注意!!!
$appsecret = '小程序公众平台中的APPSECRET'; // 注意!!!
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$output = curlGet($url);
$result = json_decode($output, true);
return $result['access_token'];
}
// 发送通知
function sendNotice(){
$access_token = getToken();
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
$post_data = [
'touser' => 'openid', // 用户的 openID,可用过 wx.getUserInfo 获取
'template_id' => 'SNZQnzYFJMwwgRp3Oh2fvI_PHp_SQWqZzpiXLP3pSJI', // 小程序后台申请到的模板编号
'page' => '/pages/index/index', // 点击模板消息后跳转到的页面,可以传递参数
'form_id' => $formid, // 第一步里获取到的 formID
'data' => [
'keyword1' => ['value' => '信息1'],
'keyword2' => ['value' => '信息2'],
'keyword3' => ['value' => '信息3']
],
'emphasis_keyword' => '' // 需要强调的关键字,会加大居中显示
];
$data = json_encode($post_data, true);
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-type:application/json', // header 需要设置为 JSON
'content' => $data,
'timeout' => 60 // 超时时间
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}

微信小程序 发送模版消息的更多相关文章

  1. 微信小程序发送模板消息

    微信小程序发送模板消息 标签(空格分隔): php 看小程序文档 [模板消息文档总览]:https://developers.weixin.qq.com/miniprogram/dev/framewo ...

  2. .netcore 3.1 C# 微信小程序发送订阅消息

    一.appsettings.json定义小程序配置信息 "WX": { "AppId": "wx88822730803edd44", &qu ...

  3. 微信小程序 发送模板消息的功能实现

    背景 - 小程序开发的过程中,绝大多数会满足微信支付 - 那么,作为友好交互的体现,自然就会考虑到支付后的消息通知咯 - 所以,我的小程序项目也要求完成这个效果,so.分享一下自己的实现步骤,以方便道 ...

  4. 微信小程序发送订阅消息(之前是模板消息)

    之前的模板消息已经废弃,现在改为订阅消息,订阅消息发布前,需要用户确认后才能接收订阅消息. 小程序端 index.wxml <button bindtap="send"> ...

  5. 微信小程序-发送模板消息(C#)

    步骤一:获取模板ID 有两个方法可以获取模版ID 通过模版消息管理接口获取模版ID 在微信公众平台手动配置获取模版ID 步骤二:页面的 <form/> 组件,属性report-submit ...

  6. 微信小程序-发送模板消息

    1 添加一个小程序的消息模板,获取到模板id,存储到数据库中,方便以后修改调用 2. https://developers.weixin.qq.com/miniprogram/dev/api-back ...

  7. 微信小程序客服消息使用指南

    客服消息使用指南 为丰富小程序的服务能力,提高服务质量,微信为小程序提供客服消息能力,以便小程序用户可以方便快捷地与小程序服务提供方进行沟通. 功能介绍 用户可使用小程序客服消息功能,与小程序的客服人 ...

  8. 微信小程序开发模板消息的时候 出现 errcode: 41028, errmsg: "invalid form id hint:

    小程序开发模板消息的时候  出现 errcode: 41028, errmsg: "invalid form id hint: 我是使用的微信支付发送模板消息,提示的formid无效的 大家 ...

  9. 微信小程序客服消息开发实战:实时在手机上接收小程序客服消息通知,以及在手机上回复

    在微信小程序开发中,可以非常方便的集成客服功能,只需要一行代码便可以将用户引导至客服会话界面.这行代码就是: <button open-type="contact" bind ...

随机推荐

  1. require.js 学习基础

    RequireJS 是一个JavaScript模块加载器,他的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代 ...

  2. web 复制功能和span光标

    参考文章:https://www.cnblogs.com/tugenhua0707/p/7395966.html https://blog.csdn.net/woshinia/article/deta ...

  3. nginx HttpLuaModule

    http://wiki.nginx.org/HttpLuaModule#Directives Name ngx_lua - Embed the power of Lua into Nginx This ...

  4. JVM内存管理基础

     JVM 虚拟机架构(图片来源: 浅析Java虚拟机结构与机制) JVM 内存区域 JVM会将Java进程所管理的内存划分为若干不同的数据区域. 这些区域有各自的用途.创建/销毁时间: (图片来源:  ...

  5. Inspector视图中的get/set使用

    using UnityEngine; using System.Collections; public class Test : MonoBehaviour { public int width { ...

  6. Java学习 第二节

    1.非递归求第四十个斐波那契数 package test; public class fibonacci2 { public static void main(String arg[]) { ; ; ...

  7. .Spark Streaming(上)--实时流计算Spark Streaming原理介

    Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍 http://www.cnblogs.com/shishanyuan/p/474 ...

  8. java多线程实例

    import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.concurr ...

  9. python面试题(转)

    下面的代码输出什么? list = ['a', 'b', 'c', 'd', 'e'] print list[10:] 上面的代码输出[],并且不会导致IndexError错误 跟你想的一样,当取列表 ...

  10. Mysql两个time类型计算时间相减

    round((UNIX_TIMESTAMP(finishtime)-UNIX_TIMESTAMP(starttime))/60) 得到的时间是分钟数