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

之前的模板消息已经废弃,现在改为订阅消息,订阅消息发布前,需要用户确认后才能接收订阅消息。

bVbLNki

小程序端

index.wxml发送订阅消息

index.jsconst app = getApp()

Page({

data: {

},

send:function(){

wx.requestSubscribeMessage({

tmplIds: ['WZiCliW1zVtHXqX7dGnFNmFvxhW-wd9S_W4WfrwNvss'],

success:(res)=> {

// 在登录的时候,获取到的openid进行缓存,现在直接把openid提取出来即可

wx.getStorage({

key: 'openid',

success (res) {

console.log(res.data)

wx.request({

url: 'https://www.xxx.com/send.php?openid='+res.data,

data: {},

header: {

'content-type': 'application/json'

},

success (res) {

// 推送

if(res.data.errcode == '43101'){

console.log("拒绝订阅消息")

}else if(res.data.errcode == '0'){

console.log("发送订阅消息")

}else{

console.log("未知错误")

}

}

})

},

fail (res) {

console.log("没有openid,无法发送")

}

})

}

})

}

})

后端<?php

//设置 header

header("Content-type:application/json");

//接收参数

$openid = $_GET["openid"];

//初始化 CURL

$ch = curl_init();

// 获取access_token

// include '';

require_once("access_token.php");

//目标服务器地址

curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token);

//设置要POST的数据

curl_setopt($ch, CURLOPT_POST, true);

$data = '{

"touser": "'.$openid.'",

"template_id": "模板ID",

"page": "pages/index/index",// 要跳转的页面

"lang":"zh_CN",

"data": {

"thing4": {

"value": "欢迎使用专插本最前线小程序"

},

"thing5": {

"value": "小程序由公众号:广东专插本最前线开发"

}

}

}';

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

// 对认证证书来源的检查

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

// 从证书中检查SSL加密算法是否存在

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

//获取的信息以文件流的形式返回,而不是直接输出

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//发起请求

$result = curl_exec($ch);

echo $result;

//关闭请求

curl_close($ch);

?>

access_token.php<?php

// 声明页面header

header("Content-type:charset=utf-8");

// APPID、APPSECRET

$appid = "你的小程序APPID";

$appsecret = "你的小程序APPSECRET";

// 获取access_token和jsapi_ticket

function getToken(){

$file = file_get_contents("access_token.json",true);//读取access_token.json里面的数据

$result = json_decode($file,true);

//判断access_token是否在有效期内,如果在有效期则获取缓存的access_token

//如果过期了则请求接口生成新的access_token并且缓存access_token.json

if (time() > $result['expires']){

$data = array();

$data['access_token'] = getNewToken();

$data['expires'] = time()+7000;

$jsonStr = json_encode($data);

$fp = fopen("access_token.json", "w");

fwrite($fp, $jsonStr);

fclose($fp);

return $data['access_token'];

}else{

return $result['access_token'];

}

}

//获取新的access_token

function getNewToken($appid,$appsecret){

global $appid;

global $appsecret;

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";

$access_token_Arr = file_get_contents($url);

$token_jsonarr = json_decode($access_token_Arr, true);

return $token_jsonarr["access_token"];

}

$access_token = getToken();

?>

逻辑

1、通过button控件出发send函数

2、send函数调用wx.requestSubscribeMessageAPI,微信允许接收订阅消息

3、wx.request向send.php后端请求

4、后端获取access_token后,调用订阅消息接口POST一段json数据即可发送订阅消息

官方文档

Author:TANKING

Date:2020-08-24

Web:http://www.likeyun.cn/

WeChat:face6009

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值