微信发送模版消息

微信发送模版消息我们需要将一些行为的进展消息推送给用户 除了短信 发送微信模板消息也是不错的选择 模板消息免费 精准到达 而且可以引导用户回到网站上来 但它有两个前提条件 1 个是认证的服务号 你才能选择模板 2 个是被推送的用户必须关注了你的公众号 而且你也拿到了他的 openid 先在模板库中找到自己的想要的模板 添加到 我的模板 中

大家好,我是讯享网,很高兴认识大家。

我们需要将一些行为的进展消息推送给用户。除了短信,发送微信模板消息也是不错的选择。模板消息免费、精准到达、而且可以引导用户回到网站上来。但它有两个前提条件。1个是认证的服务号,你才能选择模板。2个是被推送的用户必须关注了你的公众号,而且你也拿到了他的openid。


讯享网

先在模板库中找到自己的想要的模板,添加到“我的模板”中。

展开详情,我们可以看到示例。

接下来用PHP代码发送一次:

class Wechat { //首先获取获取access_token public static function get_access_token(){ $ch = curl_init(); //初始化一个CURL对象 $appId = "xxxxxxxxxxxxxxxxxx"; //微信appid $appSecret = "xxxxxxxxxxxxxxxxxx"; //微信appsecret $url = "https://api.weixin..com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret; $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$url); //要访问的地址 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证 $data = json_decode(curl_exec($ch)); if($data->access_token){ $token_file = fopen("token.txt","w") or die("Unable to open file!");//打开token.txt文件,没有会新建 fwrite($token_file,$data->access_token);//重写tken.txt全部内容 fclose($token_file);//关闭文件流 }else{ echo $data->errmsg; } curl_close($ch); } //读取access_token的方法 public static function read_token(){ $token_file = fopen("token.txt", "r") or die("Unable to open file!"); $rs = fgets($token_file); fclose($token_file); return $rs; } //发送模版消息 public function send(){ $this->build_access_token(); $ACCESS_TOKEN=$this->read_token(); $data=array( 'touser'=>"olfsB1VwJLKYsGbss90z7J-3baE4", //要发送给粉丝的openid 'template_id'=>"0C3WQsssss8pzMesCyrU5_8pm2Abmags7DydiTaOdUM",//改成自己的模板id,在微信后台模板消息里查看 'url'=>"http://www.xxxxxxx.com/weixin/", //自己网站链接url 'data'=>array( 'first'=>array('value'=>"XXX,你好",'color'=>"#fc0101"), 'keyword1'=>array('value'=>"XXX门店",'color'=>"#"), 'keyword2'=>array('value'=>"2018-12-12",'color'=>"#"), 'keyword3'=>array('value'=>"美容",'color'=>"#"), 'remark'=>array('value'=>"欢迎光临。",'color'=>"#"), ) ); $json_data=json_encode($data);//转化成json数组让微信可以接收 $url="https://api.weixin..com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;//模板消息请求URL $res=$this->https_request($url,urldecode($json_data));//请求开始 $res=json_decode($res,true); if($res['errcode']==0 && $res['errcode']=="ok"){ echo "发送成功!"; } } //curl请求函数,微信都是通过该函数请求 function https_request($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } }

讯享网
小讯
上一篇 2025-01-28 14:17
下一篇 2025-01-09 14:34

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/107996.html