在需要发送模板消息的地方引用以下方法
模板消息公共代码
<?php
define('IN_ECTOUCH', true);
require(dirname(__FILE__) . '/include/init.php');
$access_token = access_token($db);
$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
//opid要传进来
$opid = 'oAtaowa-dG87exgCAFpgTqhdaQNc';//oAtaowfDaiSi29xOlMEWxDw-kB04,oAtaowa-dG87exgCAFpgTqhdaQNc
//公众号里拿到模板ID,如果是测试号,要登录测试号添加模板
$model = 'N67dHaQEl6UV4B7pUNIGzNtq1XxXICXUFN_-l8zHNOQ';
$nickname = $db->getOne("select nickname from wxch_user where wxid = '".$opid."'");
$msg['touser'] = $opid;
$msg['template_id'] = $model;
//跳转链接,可以为空
$msg['url'] = '';//url链接
$msg['topcolor'] = '#FF0000';
$msg['data']['first'] = array('value' => '','color' => '#FF0000',);
//keyword 是模板消息动态的字段
$msg['data']['keyword1'] = array('value' => '广西','color' => '#FF0000',);
$msg['data']['keyword2'] = array('value' => '太空','color' => '#FF0000',);
$msg['data']['keyword3'] = array('value' => date("Y-m-d H:i"),'color' => '#FF0000',);
$msg['data']['keyword4'] = array('value' => $nickname,'color' => '#FF0000',);
$msg['data']['remark'] = array('value' => '','color' => '',);//提示信息
$json = json_encode($msg);
$ret_json = post($url, $json);
print_r($ret_json);exit;
function access_token($db) {
$ret = $db->getRow("SELECT * FROM `wxch_config` WHERE `id` = 1");
$appid = $ret['appid'];
$appsecret = $ret['appsecret'];
$access_token = $ret['access_token'];
$dateline = $ret['dateline'];
$time = time();
if(($time - $dateline) >= 7200) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$ret_json = curl_get_contents($url);
$ret = json_decode($ret_json);
if($ret->access_token){
$db->query("UPDATE `wxch_config` SET `access_token` = '$ret->access_token',`dateline` = '$time' WHERE `id` =1;");
return $ret->access_token;
}
}
elseif(empty($access_token)) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$ret_json = curl_get_contents($url);
$ret = json_decode($ret_json);
if($ret->access_token){
$db->query("UPDATE `wxch_config` SET `access_token` = '$ret->access_token',`dateline` = '$time' WHERE `id` =1;");
return $ret->access_token;
}
}
else { return $access_token;}
}
function get_token(){
$ch = curl_init();
$url = '';
}
function post($url,$data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
?>