模板消息

在需要发送模板消息的地方引用以下方法
模板消息公共代码

<?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;
}

?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值