我是靠谱客的博主 勤恳月光,这篇文章主要介绍微信公众号消息模版推送,现在分享给大家,希望可以做个参考。

公众号消息推送主要有三个步骤:

1.获取access_token
2.数据处理
3.发送消息

Python版本

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*- import requests import json #一.获取access_token def access_token(): appid='xxxxxxxxx' secret='xxxxxxxxxxxxx' url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appid+'&secret='+secret result=requests.get(url).text result=json.loads(result) if 'access_token' in result: access_token = result['access_token'] return access_token else: print(result['errmsg']) #二.数据处理 def template_data(openid,order_no,goods_name,goods_num,price,remark): template={ 'touser':openid, 'template_id':'s-sJehXf_HcEQQCief8VxTG6o5FwftoOtQJrsSNsz40', 'url':'https://baidu.com', 'data':{ 'first':{ "value": "订单已经付款", "color": "#173177" }, 'keyword1':{ "value": goods_num, "color": "#173177" }, 'keyword2': { "value": goods_name, "color": "#173177" }, 'keyword3': { "value": order_no, "color": "#173177" }, 'keyword4': { "value": price, "color": "#173177" }, 'remark': { "value": remark, "color": "#173177" }, } } return json.dumps(template) #三.推送消息 def send_message(data,access_token): url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='+access_token result=requests.post(url=url, data=data, files=None, headers={}) return json.loads(result.text) if __name__ == '__main__': # 一.获取access_token access_token=access_token() # 二.数据处理 data=template_data('openid','202103041505','商品名称','100.00元','测试会员','备注') # 三.推送消息 result=send_message(data,access_token) print(result)

PHP版本

一.封装curl请求

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
先封装一下后面会用到的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); //默认GET请求,参数不为空的时候用POST请求 if(!empty($data)){ curl_setopt($curl, CURLOPT_POST, 0); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); return $result; }

二.获取access_token

复制代码
1
2
3
4
5
6
7
8
9
10
$appid='appid'; $secret='secret'; $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret=$secret"; $result=Https_Request($url); $result=json_decode($result,true); $result['access_token']; //access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。 //获取到access_token应存到缓存里,设置失效时间。下次获取,先判断缓存是否存在access_token }

三.数据处理

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function data($openid=null,$order_no=null,$goods_name=null,$goods_num=null,$price=null){ $template = [ 'touser'=>$openid,//用户的openid 'template_id'=>'SudqvwsNYy4AFqBO4_KaWklYlhrc69c74jiAF3VBoCU', 'url'=>'跳转链接', 'data'=>[ 'first'=>['value'=>'订单已经付款','color'=>'#173177'], 'keyword1'=>['value'=>$order_no,'color'=>'#173177'], 'keyword2'=>['value'=>$goods_name,'color'=>'#173177'], 'keyword3'=>['value'=>$goods_num,'color'=>'#173177'], 'keyword4'=>['value'=>$price,'color'=>'#173177'], 'remark'=>['value'=>'备注','color'=>'#173177'] ] ]; return json_encode($template); }

四.发送消息

复制代码
1
2
3
4
5
6
7
function Send_Message($data,$access_token){ $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}"; $res=Https_Request($url,$data); $result=json_decode($res,true); return $result; }

在这里插入图片描述

最后

以上就是勤恳月光最近收集整理的关于微信公众号消息模版推送的全部内容,更多相关微信公众号消息模版推送内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部