当前位置:网站首页>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

边栏推荐
猜你喜欢

「精致店主理人」青年创业孵化营·首期顺德场圆满结束!

筑梦数字时代,城链科技战略峰会西安站顺利落幕

Analysis of mutex principle in golang

MySQL script batch queries all tables containing specified field types in the database
![[HFCTF2020]BabyUpload session解析引擎](/img/db/6003129bc16f943ad9868561a2d5dc.png)
[HFCTF2020]BabyUpload session解析引擎

Part IV: STM32 interrupt control programming

Installation and testing of pyflink
![[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr](/img/4a/0dcc28f76ce99982f930c21d0d76c3.png)
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr

阿里云中mysql数据库被攻击了,最终数据找回来了
![[Niuke] b-complete square](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[Niuke] b-complete square
随机推荐
Openjudge noi 1.7 08: character substitution
Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
Oracle:CDB限制PDB资源实战
Chenglian premium products has completed the first step to enter the international capital market by taking shares in halber international
第三方跳转网站 出现 405 Method Not Allowed
Analysis of mutex principle in golang
Meet in the middle
View remote test data and records anytime, anywhere -- ipehub2 and ipemotion app
The MySQL database in Alibaba cloud was attacked, and finally the data was found
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
golang中的Mutex原理解析
C # method of calculating lunar calendar date 2022
Transformation transformation operator
Body mass index program, entry to write dead applet project
Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version
What kind of experience is it to realize real-time collaboration in jupyter
Spark TPCDS Data Gen
阿里云中mysql数据库被攻击了,最终数据找回来了
Tencent cloud webshell experience
批量获取中国所有行政区域经边界纬度坐标(到县区级别)