记录一下DEMO

<?php

function getAccessToken ($appid, $appsecret) {
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$html = file_get_contents($url);
$output = json_decode($html, true);
$access_token = $output['access_token']; return $access_token;
} // 根据你的模板对应的关键字建立数组
// color 属性是可选项目,用来改变对应字段的颜色
$value = '为什么会这样测试';
$color = '#FF0000';
$data_arr = array(
'keyword1' => array( "value" => $value, "color" => $color ),
'keyword2' => array( "value" => $value, "color" => $color ),
'keyword3' => array( "value" => $value, "color" => $color ),
'keyword4' => array( "value" => $value, "color" => $color )
); $openid = 'onRuB4nGLMDTXUV-mxhlYOgV0nuY';
$templateid = 'UHttQpo6ZTkRgP93JWHmG7ChUm2aL9BCU5vzplCEtd0';
$formid = '339f1696b1544e87b3811433179f7910';
$post_data = array (
// 用户的 openID,可用过 wx.getUserInfo 获取
"touser" => $openid,
// 小程序后台申请到的模板编号
"template_id" => $templateid,
// 点击模板消息后跳转到的页面,可以传递参数
"page" => "/pages/app/init/main",
// 第一步里获取到的 formID
"form_id" => $formid,
// "prepay_id" => $formid,
// 数据
"data" => $data_arr,
// 需要强调的关键字,会加大居中显示
"emphasis_keyword" => "keyword2.DATA" ); // 发送 POST 请求的函数
// 你也可以用 cUrl 或者其他网络库,简单的请求这个函数就够用了
function send_post( $url, $post_data ) {
$options = array(
'http' => array(
'method' => 'POST',
// header 需要设置为 JSON
'header' => 'Content-type:application/json',
'content' => $post_data,
// 超时时间
'timeout' =>
)
); $context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context ); return $result;
} // 这里替换为你的 appID 和 appSecret
$appid = 'xxxx';
$appsecret = 'xxx';
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".getAccessToken ($appid, $appsecret);
// 将数组编码为 JSON
$data = json_encode($post_data, true); // 这里的返回值是一个 JSON,可通过 json_decode() 解码成数组
$return = send_post( $url, $data);
var_dump($return);

成功返回:

string(27) "{"errcode":0,"errmsg":"ok"}"

同时微信上也会收到模板消息

PHP 小程序发模板消息的更多相关文章

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

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

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

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

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

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

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

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

  5. 微信小程序:模板消息推送提示{“errcode”:41030,”errmsg”:”invalid page hint: [gP1eXXXXXX]”}

    在开发小程序 模板消息定时推送功能时,在开发版测试程序功能运行正常,但提交到线上后提示报错{“errcode”:41030,”errmsg”:”invalid page hint: [gP1eXXXX ...

  6. 微信小程序的模板消息与小程序订阅消息

    小程序订阅消息 功能介绍 消息能力是小程序能力中的重要组成,我们为开发者提供了订阅消息能力,以便实现服务的闭环和更优的体验. 订阅消息推送位置:服务通知 订阅消息下发条件:用户自主订阅 订阅消息卡片跳 ...

  7. Python 发送微信小程序的模板消息

    在小程序的开发过程中,会存在模板消息的发送,具体文档见 这里,模板消息的发送是和语言无关的,这里将简要写一下怎么用 Python 给用户发送模板消息.     通过文档可以知道,发送的时候,需要使用小 ...

  8. 微信小程序之模板消息推送

    最近在用sanic框架写微信小程序,其中写了一个微信消息推送,还挺有意思的,写了个小demo 具体见官方文档:https://developers.weixin.qq.com/miniprogram/ ...

  9. 订阅消息---由于微信小程序取消模板消息,限只能开发订阅消息

    订阅消息开发步骤: 1.小程序管理后台添加订阅消息的模板 2.小程序前端编写调用(拉起)订阅授权 wx.requestSubscribeMessage({ tmplIds: ['34fwe1211xx ...

随机推荐

  1. mysql 数据库的时间与字符串转换

    .当前日期.时间 now() 获取 当前日期和时间 :: curdate() 当前日期, curtime() 当前时间 :: current_time() : //同curtime(),current ...

  2. linux系统编程之进程(四)

    今天继续研究进程相关的东东,话不多说,进入正题: SIGCHLD: 关于它,之前章节的学习中已经用到了,具体可以参考博文:http://www.cnblogs.com/webor2006/p/3500 ...

  3. Java&Selenium自动化测试之Page Object Model

    PO是什么: 1.页面对象模型(PO)是一种设计模式,用来管理维护一组web元素的对象库 2.在PO下,应用程序的每一个页面都有一个对应的page class 3.每一个page class维护着该w ...

  4. 二叉树(python)

    # -*- coding: utf-8 -*- from collections import deque class Queue(object): def __init__(self): self. ...

  5. ASP.NET MVC 入门11、使用AJAX

    asp.net mvc 支持微软自身Ajax 和 JQuery框架 asp.net mvc View视图可以理解为 一个包含"<%%>"变量引和的模板. Script与 ...

  6. git基础问题

    1).git add 与gitstage的区别 git stage只是git add的同义词,所以在使用上没有区别 i)Git仓库的三个组成部分:工作区(Working Directory).暂存区( ...

  7. windows nginx 目录配置

    http { server { listen 80; server_name www.test.com; location / { root E:/data/www; index index.html ...

  8. GreenPlum 常用命令

    gpstate 命令 参数 作用 gpstate -b => 显示简要状态 gpstate -c => 显示主镜像映射 gpstart -d => 指定数据目录(默认值:$MASTE ...

  9. 2019/12/5BJFirstDay--scrum后台+cpp项目前台环境跑起来!!!

    1.配置服务器: 2.进入cd C:\java\25.beijing\06.vuejs\cpp201911221829\cpp 3.运行的命令是:npm run dev 4.先启动 5.然后再启动cp ...

  10. 洛谷 P2627 修剪草坪 题解

    P2627 修剪草坪 题目描述 在一年前赢得了小镇的最佳草坪比赛后,Farm John变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,Farm John希望能够再次夺冠. 然而,Fa ...