当前位置:网站首页>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
- JTAG principle of arm bare board debugging
- How to evaluate load balancing performance parameters?
- 【JVM调优实战100例】04——方法区调优实战(上)
- taro3.*中使用 dva 入门级别的哦
- Part VI, STM32 pulse width modulation (PWM) programming
- golang中的Mutex原理解析
- Atomic in golang and CAS operations
- Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax
- 树莓派/arm设备上安装火狐Firefox浏览器
猜你喜欢
省市区三级坐标边界数据csv转JSON
MySQL script batch queries all tables containing specified field types in the database
Can the system hibernation file be deleted? How to delete the system hibernation file
阿里云中mysql数据库被攻击了,最终数据找回来了
[Niuke] b-complete square
让我们,从头到尾,通透网络I/O模型
Chenglian premium products has completed the first step to enter the international capital market by taking shares in halber international
Installation and testing of pyflink
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
[case sharing] basic function configuration of network loop detection
随机推荐
Neon Optimization: an instruction optimization case of matrix transpose
[HFCTF2020]BabyUpload session解析引擎
Force buckle 1037 Effective boomerang
Pytorch中torch和torchvision的安装
UI control telerik UI for WinForms new theme - vs2022 heuristic theme
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
Deep learning framework TF installation
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
Summary of being a microservice R & D Engineer in the past year
[牛客] [NOIP2015]跳石头
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
「笔记」折半搜索(Meet in the Middle)
[hfctf2020]babyupload session parsing engine
tensorflow 1.14指定gpu运行设置
Taro2.* 小程序配置分享微信朋友圈
Dynamic planning idea "from getting started to giving up"
What kind of experience is it to realize real-time collaboration in jupyter
Telerik UI 2022 R2 SP1 Retail-Not Crack
MySQL中回表的代价
Dell笔记本周期性闪屏故障