微信公众号模板消息 简易完整代码

<?php
function curlhttp($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

 //moban
function moban($plan,$user,$time,$desc,$openid) {

 //使用前请 修改以下3个参数
 $appid="wxce3b9bd********c"; //appid
 $appsecret="2efaac3bb2********af177683866f69"; //appsecret
 $template_id = "9qUKfOTQ********sQBlMxLqoV2xvIH7Kl1iRUySYfI";

 $filetoka = "toka.txt";
 $daysnows = date('Y-m-d H:i:s');
 $daysides = date('YmdHis');
if(file_exists($filetoka)){
 $ttimes = filemtime($filetoka);
 $ftimes = date('Y-m-d H:i:s', $ttimes);
}else{
 $ftimes = date("Y-m-d H:i:s",strtotime("-1 day"));
}
 echo "<b>ktime</b>$ftimes";
$tiold=strtotime($ftimes);
$tinew=strtotime($daysnows);
$seyu=floor($tinew-$tiold);
if($seyu<600){ //60=1分钟
 $ACCESS_TOKEN = file_get_contents($filetoka);
 echo "cache token = '{$ACCESS_TOKEN}@{$ftimes}';\r\n";
}else{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
$url .= "&appid=$appid&secret=$appsecret";
 $json = file_get_contents($url);
 $result=json_decode($json,true);
 $ACCESS_TOKEN=$result['access_token'];
if($ACCESS_TOKEN){
 echo "news token = '{$ACCESS_TOKEN}@{$ftimes}';\r\n";
 file_put_contents($filetoka,$ACCESS_TOKEN);
}else{
 echo "kong token = '{$json}@{$ftimes}';\r\n"; //没有token
 file_put_contents($filetoka.".toka.kong.txt",$json);
}

}

 //模板消息 可修改
 $template=array(
 'touser'=>$openid,
 'template_id'=>$template_id, //模板的id
 'url'=>"http://add.12391.net/?d=".$daysides,
 'topcolor'=>"#FF0000",
 'data'=>array(
 'first'=>array('value'=>urlencode($plan),'color'=>"#00008B"), 
 'keyword1'=>array('value'=>urlencode($user),'color'=>"#00008B"), 
 'keyword2'=>array('value'=>urlencode($time),'color'=>'#00008B'), 
 'remark'=>array('value'=>urlencode($desc),'color'=>'#00008B')
 )
 );

$json_template=json_encode($template);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;
$res=curlhttp($url,urldecode($json_template));
$sms=json_decode($res,true);
$isok=$sms['errmsg'];
echo "<h3>json_template</h3>".$json_template;
if($isok == "ok"){
echo "<h3>fa ok</h3>".$res; //OK的返回值
}else{
echo "<h3>fa er</h3>".$res; //ER的返回值
 unlink($filetoka); //尝试删除缓存token并记录
 file_put_contents($filetoka.".back.erer.txt",$res);
}

}


/*
模板库找到模板添加
编号 OPENTM411626253
标题 服务到期提醒
行业 IT科技 - IT软件与服务
最后修改时间 2017-08-01 15:38:22

或者

添加的模板信息详细内容:
{{first.DATA}}
机构名称:{{keyword1.DATA}}
服务期限:{{keyword2.DATA}}
{{remark.DATA}}

*/

//plan user time userid 将以下内容修改特别别是最后个参数userid为自己公众号下存在的用户ID
moban("开头备注test","我自己","2019年12月12日","后续备注","olwyOwk28TMY-exD3omLVB3L6awg");
 
?>

使用注意:

1.使用前请 修改以3个参数详见中文名字

2.最后一行代码 最后个参数务必修改自己公众号下的userid测试可用管理员自己的

3.认证公众号后台添加模板见代码内汉字说明

4.二次修改自网络源码,增加token缓存与自动更新机制,完善说明书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YUJIANYUE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值