微信小程序 : 发送模板消息

1.开启模板消息

登录小程序 → 设置 → 开发设置 → 消息推送 → 启用

服务地址 : www.123.com/index.php/index/checkSignature

Token : 123456

密匙 : 自定义

加密方式 : 兼容

数据格式 : 随意

private function checkSignature()
{
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];

    $token = '123456';
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );

    if( $tmpStr == $signature ){
        return true;
    }else{
        return false;
    }
}

2.小程序用户提交发送消息

wxml

<form name='pushMsgFm' report-submit bindsubmit='submit'>
  <button form-type="submit">提交</button>
</form>   

js

//  提交
submit: function(e) {
  var that = this;
  var formId = e.detail.formId; //  获取formId
  wx.request({
    url: 'add',
    method: 'POST',
    header: { "content-type": "application/x-www-form-urlencoded" },
    data: {
      user_id: wx.getStorageSync('user_id'),
      formId: formId
    },
    success: function (res) {
      //  回调
    }
  });
}

php

public function add() {
  $userId = input('user_id');
  $user = db('user')->where('id',$userId)->find();
  $formId = input('formId');
  $this->smallWXmessage($user['open_id'],$formId);
}
private function smallWXmessage($openId,$formId) {
  $data = <<<END
      {
          "touser": "$openId",
          "template_id": "模板ID",
          "page": "index",
          "form_id": "$formId",
          "data": {
              "keyword1": {
                  "value": "name"
              },
              "keyword2": {
                  "value": "phone"
              }
          },
          "emphasis_keyword": "keyword1.DATA"
        }
END;
      
  $str = $this->getToken();
  $token = substr($str,strpos($str,'{'));
  $access = json_decode($token,true);  //获取token
  $access_token= $access['access_token'];
  $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $access_token;
  $res = $this->getHttpArray($url,$data);  //post请求url
  return $res;
}
//  获取TOKEN
private function getToken()
{
    $appid = APPID;
    $secret = secret;
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
    $data = $this->curlGet($url);
    return $data;
}
private function curlGet($url) {
  //初始化
  $curl = curl_init($url);
  //设置抓取的url
  curl_setopt($curl, CURLOPT_URL, $url);
  //设置头文件的信息作为数据流输出
  curl_setopt($curl, CURLOPT_HEADER, 1);
  //设置获取的信息以文件流的形式返回,而不是直接输出。
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  //执行命令
  $data = curl_exec($curl);
  //关闭URL请求
  curl_close($curl);

  return $data;
}
//  通过post请求发送消息模板
private function getHttpArray($url,$post_data) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出,不用print_r();也会在后面多个1
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  $output = curl_exec($ch);
  curl_close($ch);
  $out = json_decode($output);
  return $out;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值