解决微信小程序发送模板消息(php后台)

小程序已经可以用来做商家的业务,实现模板消息的发送就很有必要了
如:
63988-wgxyswcftij.png

首先是小程序端:

首先是表单提交获取formid;

wxml文件代码:

<form bindsubmit="submitForm" report-submit >//一定要有report-submit ,不然获取不到formid
    <view class="form_group">
        <text>姓  名:</text>
        <input type="text" placeholder="请输入姓名" name="data_name" maxlength="20" value="" auto-focus/>
    </view>
    <button class="save_btn" form-type="submit">确认参加</button>
</form>

js文件代码:

submitForm:function(e){
    var name = e.detail.value.data_name;
    var formid = e.detail.formid;//开发工具里调试的时候显示的是the formId is a mock one,要到真机才能看到formid的值
    var tarPage = "/haa/muban";//点击消息跳转的界面
    wx.request({
      url: 'http://localhost/test/temMsg.php',//服务器地址
      data:{
        formid:formid,
        name:name,
        openid:wx.getStorageSync("openid"),//要获取用户的openid才能发送,如果还不会建议看上篇;
      },
      header:{
        "Content-type":"application/json",
      },
      success:function(res){
        console.log(res.data);
      },
      fail:function(err){
        console.log(err);
      }
    })
  }

后台php代码:

<?php
include 'getcurl.php';//封装了发送get请求的php
include 'postCurl.php';//封装了发送post请求的php
temMsg();
function temMsg()
{
    $name = $_GET['name'];
    $formid = $_GET['formid'];
    $temid = 'nDN7NDX5BB_Dd9PcM7SDlNWGQdPDTIfEf7kloG10E2Q';
    $page = '/haa/muban';
    $openid = $_GET['openid'];
    if(!$openid||!$formid)die('failed!');
    $key1 = '111111';//发送的消息
    $key2 = $name;
    $key3 = '3月28日12:00-3月30日12:00';
    $key4 = '联系电话:134135415';
    $key5 = '猫萌萌抱枕';
    $key6 = 'xxx';
    $access_token = returnAssKey();
    $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
    $data = array(//这里一定要按照微信给的格式
        "touser"=>$openid,
        "template_id"=>$temid,
        "page"=>$page,
        "form_id"=>$formid,
        "data"=>array(
            "keyword1"=>array(
                "value"=>$key1,
                "color"=>"#173177"
            ),
            "keyword2"=>array(
                "value"=>$key2,
                "color"=>"#173177"
            ),
            "keyword3"=>array(
                "value"=>$key3,
                "color"=>"#173177"
            ),
            "keyword4"=>array(
                "value"=>$key4,
                "color"=>"#173177"
            ),
            "keyword5"=>array(
                "value"=>$key5,
                "color"=>"#173177"
            ),
            "keyword6"=>array(
                "value"=>$key6,
                "color"=>"#173177"
            )
        ),
        "emphasis_keyword"=>"keyword1.DATA",//需要进行加大的消息
    );
    $res = postCurl($url,$data,'json');//将data数组转换为json数据
    if($res){
        echo json_encode(array('state'=>4,'msg'=>$res));
    }else{
        echo json_encode(array('state'=>5,'msg'=>$res));
    }
 
}
 
function returnAsskey()
{
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=yourAppid&secret=yourSecert';
    $ass_key = curl_get($url);
    $a1 = $ass_key->access_token;
    return $a1;
}
?>

postCurl.php文件代码:

<?php
 
function postCurl($url,$data,$type)
{
    if($type == 'json'){
        $data = json_encode($data);//对数组进行json编码
        $header= array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
    }
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_POST,1);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
    if(!empty($data)){
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    }
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
    $res = curl_exec($curl);
    if(curl_errno($curl)){
        echo 'Error+'.curl_error($curl);
    }
    curl_close($curl);
    return $res;
}
?>

getCurl.php文件代码:

<?php
function curl_get($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    return json_decode($data);//对数据进行json解码
}
?>

如果在开发工具上进行调试会出现41028的错误,是因为获取不到formid,可以到手机端调试然后在console里面看到formid添到开发平台上面就可以了还有就是access_token的时间期限是7200s,到期就要更换,我是直接将时间和access_token,然后每次判断一下当前时间和数据库中存取的时间是否差7200s,到了就更换,感觉这方法很蠢,才刚学php也不会很多,欢迎交流。最后成功得到通知:

10235-vyxotzfpu9.png

出处:https://blog.csdn.net/zhouzzz000/article/details/79681515



请为这篇文章评分:
( 已有 1 人评分, 平均得分: 5 分 )

标签: 微信 微信小程序 消息推送 模板消息推送 微信模板消息 php

发表评论: