云服务器 99 / 年,新老同享(可以99/年续费),开发者力荐特惠渠道,新客户在享受9折
阿里云推广

微信公众号模板消息接口

  • 内容
  • 评论
  • 相关

模板消息效果:

670431142066314955.jpg


1.首先在公众号后台添加模板消息(我的是测试公众号):

360截图20170119202404290.jpg

 模板内容:


{{qq1.DATA}} 购买商品:{{qq2.DATA}} 领奖金额:{{qq3.DATA}} 领奖时间:{{qq4.DATA}} {{qq5.DATA}}


PHP代码:

/*
 * 获取access_token
 */
function getAccessToken($appid,$appsecret)
{
    // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
    $data = json_decode(get_php_file("access_token.php"));
    if ($data->expire_time < time()) {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
        $res = json_decode(httpGet($url));
        $access_token = $res->access_token;
        if ($access_token) {
            $data->expire_time = time() + 7000;
            $data->access_token = $access_token;
            set_php_file("access_token.php", json_encode($data));
        }
    } else {
        $access_token = $data->access_token;
    }
    return $access_token;
}

/*
 *设置access_token
 */
function set_php_file($filename, $content)
{
    $fp = fopen($filename, "w");
    fwrite($fp, "<?php exit();?>" . $content);
    fclose($fp);
}

/*
 *获取access_token
 */
function get_php_file($filename)
{
    return trim(substr(file_get_contents($filename), 15));
}


/*
 * 发送post请求
 */
function curlPost($url, $data){
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    //$errorno=curl_errno($ch);
    return $tmpInfo;
}

/*
 * 发送get请求
 */
function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}


//开始推送消息   
$openid="otWC1s1N0IR-mtPGzM7d2hPXRy20";      //openid给谁发送消息
$template_id="RlqORWyAuMZ7dNqjaDguVvSkVS1O4Y0zkmio1qUOKyI";
$tempurl="https://www.wlphp.com";  
$appid="wx505d80be911dd14a";        //公众号appid
$appsecret="d6492e2728eab977232cde1f49fa51c6";  //公众号appsecret
$access_token=getAccessToken($appid,$appsecret);  //获取access_token       
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;

$json='{
           "touser":"'.$openid.'",   
           "template_id":"'.$template_id.'",
           "url":"'.$tempurl.'",            
           "data":{

                   "qq1": {
                       "value":"恭喜你购买成功!",
                       "color":"red"
                   },
                   "qq2":{
                       "value":"智商充值卡",
                       "color":"blue"
                   },
                   "qq3": {
                       "value":"250元",
                       "color":"green"
                   },
                   "qq4": {
                       "value":"2017年01月19日",
                       "color":"purple"
                   },
                   "qq5":{
                       "value":"欢迎再次购买!",
                       "color":"black"
                   }
           }
       }';   
$rs=curlPost($url, $json);  //把这个json  post给这个url
var_dump($rs);  //打印回值

成功返回结果:

string(45) "{"errcode":0,"errmsg":"ok","msgid":413046773}"

本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:微信公众号模板消息接口 - https://wziyi.net/?post=115

发表评论

电子邮件地址不会被公开。 必填项已用*标注