小程序 模板消息通知

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;
}
/************************************************/
	//表单id
	$formId=$_GET["formId"];
	//购买地点
	$site=$_GET["site"];
	//购买时间
	$time=$_GET["time"];
	//购买商品名称
	$name=$_GET["name"];
	//用户唯一标识
	$openId=$_GET["openid"];
	//交易单号
	$seats="1314";
	//模板消息id
  $templateId="";
	//字体颜色
	$color="#173177";
	//小程序id
	$appid="";
	//秘钥
	$secret="";
$data_arr = array(
  'keyword1' => array( "value" => $site, "color" => $color ),
  'keyword2' => array( "value" => $time, "color" => $color ),
  'keyword3' => array( "value" => $name, "color" => $color ),
  'keyword4' => array( "value" => $seats, "color" => $color ),
  //这里根据你的模板对应的关键字建立数组,color 属性是可选项目,用来改变对应字段的颜色
);
$post_data = array (
  "touser"           => $openId,
  //用户的 openID,可用过 wx.getUserInfo 获取
  "template_id"      => $templateId,
  //小程序后台申请到的模板编号
  "page"             => "/pages/index/index",
  //点击模板消息后跳转到的页面,可以传递参数
  "form_id"          => $formId,
  //第一步里获取到的 formID
  "data"             => $data_arr,
  "emphasis_keyword" => "keyword3.DATA"
  //需要强调的关键字,会加大居中显示
);
function send_post($url,$post_data ) {
	$options = array(
		'http' => array(
		'method'  => 'POST',
		'header'  => 'Content-type:application/json',
		//header 需要设置为 JSON
		'content' => $post_data,
		'timeout' => 60
		//超时时间
		)
	);
	$context = stream_context_create($options );
	$result = file_get_contents($url,false,$context );
	return $result;
}
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".getAccessToken($appid,$secret);  
//这里替换为你的 appID 和 appSecret
$data = json_encode($post_data,true);   
//将数组编码为 JSON
$return = send_post($url,$data);
echo '返回值:' .$return;
//这里的返回值是一个 JSON,可通过 json_decode() 解码成数组

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值