注册

微信开发之模板消息发送
首页 > IT计算机 > 微信开发    作者:RainFly   2018年1月3日 14:15 星期三   热度:5480°   字号:   评论:0    
时间:2018-1-3 14:15   热度:5480°  评论:0 条 

第一步:申请公众号测试接口

    1.jpg

第二步:添加测试用户

    1.jpg



第三步:添加消息模板

1.jpg

代码:获取动态ACCESS_TOKEN (有效期7200)

$appid='wxcfd6d103e4d5b35d';
$secret='60504239fe7597a9653b6272ae149369';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);
$access = $token["access_token"];
print_r($token);
print_r("\n");



发送模板消息

一定要注意json格式 ,否则会返回POST数据格式不正确

$url ="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access;
$data1 = '{
    "touser":"o4f0Wv5nRBQMCzZ1AsDlq_9OsOrU",
    "template_id":"HMnswJkdFHz_ZcINVzpuYE88PGMH20j4Rybr9BVfcjA",
    "url":"http://www.rainfly.cn",
    "data":{"frist":{"value":"提醒事件已经到了!","color":"#ff0000"}}
    }';
function send_post($url, $post_data) {

    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $post_data,
            'timeout' => 15 * 60 // 超时时间(单位:s)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    //file_put_contents('access.json', $result);
    //print_r($result);
    //return $result;
}
send_post($url, $data1);


测试截图

1.jpg



  您阅读这篇文章共花了:  
捐赠支持:如果觉得这篇文章对您有帮助,请 "扫一扫"鼓励作者!
二维码加载中...
本文作者:RainFly      文章标题: 微信开发之模板消息发送
本文地址:http://www.rainfly.cn/?post=298
版权声明:若无注明,本文皆为“雨夜轩”原创,转载请保留文章出处。

赶紧抢沙发哟!

QQ游客评论

返回顶部    首页    捐赠支持    手气不错    友情链接    关于我们    站长工具    站长介绍    手机版本    后台登陆   
版权所有:雨夜轩    站长:RainFly    特别鸣谢   文章归档   皖ICP备15003600号-1   百度统计
Copyright©2015 雨夜轩 Powered by emlog强力驱动 七牛CDN全球加速 360站长联盟安全认证 中国博客联盟荣誉成员 可信赖网站 站点地图   
页面加载耗时:0.022秒 数据库查询次数:11次
背景设置