微信模板消息


1、首先需要一個服務號

2、申請模板消息功能插件(一般1-3個工作日)如下圖 ,沒有的請添加,有的請忽略

3、創建適合自己的模板,拿到模板id,如下圖

 

4、后台php寫一個模板消息類

<?php
namespace Think;
class Oauth{
  //獲得全局access_token
  public function get_token(){
    $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret"; //請求地址
    //2初始化curl請求
    $ch = curl_init();
    //3.配置請求參數
    
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    
    //4.開始請求
    $res = curl_exec($ch); //獲取請求結果
    if( curl_errno($ch) ){
      var_dump( curl_error($ch) ); //打印錯誤信息
    }
    //5.關閉curl
    curl_close( $ch );
    $arr = json_decode($res, true); //將結果轉為數組
    //$_SESSION['access_token']=$arr['access_token'];  //將access_token存入session中,可以不存,每次都獲得新的token
    //$_SESSION['expire_time']=time()+7200;
    return $arr['access_token'];
    //}
  }
  //推送模板信息  參數:發送給誰的openid,客戶姓名,客戶電話,推薦樓盤(參數自定)
  function sendMessage($openid,$name,$tempid,$url,$content,$key1) {
    //獲取全局token
    $token = $this->get_token();
    $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token; //模板信息請求地址
    //發送的模板信息,根據實際需求(微信要求json格式,這里為數組(方便添加變量)格式,然后轉為json)
    $post_data = array(
           'touser'=>$openid,//openid
           'template_id'=>$tempid,
           'url'=>$url,
           'data'=>array(
               'first'=>array("value"=>$content,"color"=>"#173177"),
                  'keyword1'=>array("value"=>$name,"color"=>"#173177"),
                  'keyword2'=>array("value"=>$key1,"color"=>"#173177"),
                  'remark'=>array("value"=>"感謝你的參與!","color"=>"#173177"),
          )
      );
    //將上面的數組數據轉為json格式
    $post_data = json_encode($post_data);
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    //接收執行返回的數據
    $data = curl_exec($ch);
    //關閉句柄
    curl_close($ch);
    $data = json_decode($data,true); //將json數據轉成數組
    return $data;
  }
  
}

 php后台接口

public function premitVideo(){
//         初定義0為已審核 1為待審核 -1為拒絕
        $id=I("post.id");
        $where['id']=$id;
        $update=array("state"=>"0");
        $res=M("rt")->where($where)->save($update);
        $where['id']=I("post.id");
       $data=M("rt")->where($where)->find();
       $openid=$data['openid'];
       $name=$data["name"];
       $tempid="Q19vdlsOeJ1DafsymnZhhFFIDJWIgRXo-qwQs5vfFys";
       $url="你想讓用戶點擊后跳轉的url";
       $content="親,恭喜您!您上傳的作品已經通過審核,快去找小伙伴拉票吧!";
       $key1="通過";
       $send=new Oauth();
       $send->sendMessage($openid,$name,$tempid,$url,$content,$key1);
        if($res){
           $this->ajaxReturn(['code'=>200,'data'=>[],'msg'=>'操作成功']);
        }
    }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM