微信模板消息
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'=>'操作成功']);
}
}

浙公网安备 33010602011771号