当前位置:网站首页>Send template message via wechat official account
Send template message via wechat official account
2022-07-07 01:16:00 【Little Hier】
First step acquisition token
// The official account was obtained token
public function getGzhAccessToken_wx($appId,$appSecret){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
$html = file_get_contents($url);
$output = json_decode($html,true);
$access_token = $output['access_token'];
return $access_token;
}
Step 2: get all users who follow openid list , Save in data sheet
// Get all users openid
public function getUserList($token){
$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token={$token}&next_openid=";
$html = file_get_contents($url);
$data = json_decode($html,true);
return $data;
}
The third step is to pass openid Get basic user information
// Get basic user information
public function getUserInfo($token,$openid){
$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$openid}&lang=zh_CN";
$html = file_get_contents($url);
$data = json_decode($html,true);
return $data;
}
Step 4 send the template message
public function sendUser($token){
$data=[
"touser"=>'oXhpA1UWjrvMost8kdAuiHIZUomA', // The other person's openid, Get in the previous step
"template_id"=>"Gphar4zkxFMSvdOJ-Hx6UvbnCaM_viZiBUIZTcH5tRU", // Templates id
// "miniprogram"=>["appid"=>"wxd35c1649beb11bba", // Jump applet appid
// "pagepath"=>"pages/index/nindex/main"
// ],// Jump to applet page
"data"=>[
"first"=>[
"value"=> " Test information ", // Custom parameters
"color"=> '#173177'// Custom color
],
"keyword1"=>[
"value"=>' Che Meijia ', // Custom parameters
"color"=> '#173177'// Custom color
],
"keyword2"=>[
"value"=> date('Y-m-d H:i:s'), // Custom parameters
"color"=> '#173177'// Custom color
],
"remark"=>[
"value"=> " If there is any change , Please take the initiative to contact the user ", // Custom parameters
"color"=> '#173177'// Custom color
],
]
];
$gzh_url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
$res=json_decode($this->curl_post_wx($gzh_url,json_encode($data)),true);
return $res;
}
/**
* @param $url
* @param array $data
* @return mixed
* curl request
*/
public function curl_post_wx($url , $data=array()){
$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);
if (curl_errno($ch)) {
return false;
}else{
return $tmpInfo;
}
}
The successful sending is shown in the figure below
边栏推荐
- 【案例分享】网络环路检测基本功能配置
- C # method of calculating lunar calendar date 2022
- [Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
- gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
- Dell笔记本周期性闪屏故障
- JTAG principle of arm bare board debugging
- Implementation principle of waitgroup in golang
- Openjudge noi 1.7 10: simple password
- Dell筆記本周期性閃屏故障
- LLDP兼容CDP功能配置
猜你喜欢
[HFCTF2020]BabyUpload session解析引擎
BFS realizes breadth first traversal of adjacency matrix (with examples)
Boot - Prometheus push gateway use
Make a simple graphical interface with Tkinter
让我们,从头到尾,通透网络I/O模型
[case sharing] basic function configuration of network loop detection
Niuke cold training camp 6B (Freund has no green name level)
Dell Notebook Periodic Flash Screen Fault
第三方跳转网站 出现 405 Method Not Allowed
Tensorflow GPU installation
随机推荐
《安富莱嵌入式周报》第272期:2022.06.27--2022.07.03
Make a simple graphical interface with Tkinter
Tensorflow GPU installation
[Niuke] [noip2015] jumping stone
JTAG principle of arm bare board debugging
Atomic in golang, and cas Operations
Batch obtain the latitude coordinates of all administrative regions in China (to the county level)
系统休眠文件可以删除吗 系统休眠文件怎么删除
让我们,从头到尾,通透网络I/O模型
ARM裸板调试之JTAG调试体验
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
Neon Optimization: performance optimization FAQ QA
Address information parsing in one line of code
负载均衡性能参数如何测评?
接收用户输入,身高BMI体重指数检测小业务入门案例
微信公众号发送模板消息
如何管理分布式团队?
The difference between spin and sleep
Taro 小程序开启wxml代码压缩
golang中的atomic,以及CAS操作