当前位置:网站首页>微信公众号发送模板消息
微信公众号发送模板消息
2022-07-06 17:31:00 【小Hier】
第一步获取token
//公众号获取 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;
}第二步获取所有关注的用户openid列表,存入数据表
//获取所有用户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;
}第三步通过openid获取用户基本信息
//获取用户基本信息
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;
}第四步发送模板消息
public function sendUser($token){
$data=[
"touser"=>'oXhpA1UWjrvMost8kdAuiHIZUomA', //对方的openid,前一步获取
"template_id"=>"Gphar4zkxFMSvdOJ-Hx6UvbnCaM_viZiBUIZTcH5tRU", //模板id
// "miniprogram"=>["appid"=>"wxd35c1649beb11bba", //跳转小程序appid
// "pagepath"=>"pages/index/nindex/main"
// ],//跳转小程序页面
"data"=>[
"first"=>[
"value"=> "测试信息", //自定义参数
"color"=> '#173177'//自定义颜色
],
"keyword1"=>[
"value"=>'车美佳', //自定义参数
"color"=> '#173177'//自定义颜色
],
"keyword2"=>[
"value"=> date('Y-m-d H:i:s'), //自定义参数
"color"=> '#173177'//自定义颜色
],
"remark"=>[
"value"=> "如有变动,请主动联系用户", //自定义参数
"color"=> '#173177'//自定义颜色
],
]
];
$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请求
*/
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;
}
}发送成功如下图

边栏推荐
- Realize incremental data synchronization between MySQL and ES
- 第四篇,STM32中断控制编程
- Tensorflow 1.14 specify GPU running settings
- 第五篇,STM32系统定时器和通用定时器编程
- Rainstorm effect in levels - ue5
- 如何管理分布式团队?
- A brief history of deep learning (I)
- Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
- 动态规划思想《从入门到放弃》
- 「精致店主理人」青年创业孵化营·首期顺德场圆满结束!
猜你喜欢

Tensorflow GPU installation
![[Niuke] b-complete square](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[Niuke] b-complete square

力扣1037. 有效的回旋镖

The MySQL database in Alibaba cloud was attacked, and finally the data was found

城联优品入股浩柏国际进军国际资本市场,已完成第一步
![[case sharing] basic function configuration of network loop detection](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[case sharing] basic function configuration of network loop detection

Dell笔记本周期性闪屏故障

第七篇,STM32串口通信编程

线段树(SegmentTree)

让我们,从头到尾,通透网络I/O模型
随机推荐
NEON优化:性能优化常见问题QA
批量获取中国所有行政区域经边界纬度坐标(到县区级别)
Do you understand this patch of the interface control devaxpress WinForms skin editor?
boot - prometheus-push gateway 使用
力扣1037. 有效的回旋镖
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
Build your own website (17)
Boot - Prometheus push gateway use
深度学习简史(一)
Periodic flash screen failure of Dell notebook
JTAG principle of arm bare board debugging
Dynamic planning idea "from getting started to giving up"
NEON优化:矩阵转置的指令优化案例
负载均衡性能参数如何测评?
MySQL中回表的代价
THREE.AxesHelper is not a constructor
NEON优化:log10函数的优化案例
[Niuke] [noip2015] jumping stone
Case development of landlord fighting game