关于php:微信小程序发送订阅消息之前是模板消息

6次阅读

共计 2960 个字符,预计需要花费 8 分钟才能阅读完成。

之前的模板音讯曾经废除,当初改为订阅音讯,订阅音讯公布前,须要用户确认后能力接管订阅音讯。

小程序端

index.wxml

<button bindtap="send"> 发送订阅音讯 </button>

index.js

const app = getApp()
Page({data: {},send:function(){
    wx.requestSubscribeMessage({tmplIds: ['WZiCliW1zVtHXqX7dGnFNmFvxhW-wd9S_W4WfrwNvss'],  
      success:(res)=> {
        wx.request({
          url: 'https://www.xxx.com/send.php',
          data: {
            openid:'要推送的 openid',
            template_id:'要应用的 template_id',
          },
          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("未知谬误")
            }
          }
        })
      }
    })
  }
)}

后端

<?php
// 设置 header 
header("Content-type:application/json");

// 接管参数
$template_id = $_GET["template_id"];
$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": $template_id,"page":"pages/index/index",// 要跳转的页面"miniprogram_state":"developer","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 数据即可发送订阅音讯

官网文档

1、https://developers.weixin.qq.com/miniprogram/dev/api/open-api/subscribe-message/wx.requestSubscribeMessage.html

2、https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html

Author:TANKING
Date:2020-08-24
Web:http://www.likeyun.cn/
WeChat:face6009

正文完
 0