当前位置:网站首页>微信公众号发送模板消息
微信公众号发送模板消息
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;
}
}
发送成功如下图
边栏推荐
- Case development of landlord fighting game
- Provincial and urban level three coordinate boundary data CSV to JSON
- golang中的WaitGroup实现原理
- NEON优化:性能优化经验总结
- Summary of being a microservice R & D Engineer in the past year
- 资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
- [牛客] B-完全平方数
- [JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
- Analysis of mutex principle in golang
- BFS realizes breadth first traversal of adjacency matrix (with examples)
猜你喜欢
[case sharing] basic function configuration of network loop detection
身体质量指数程序,入门写死的小程序项目
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
筑梦数字时代,城链科技战略峰会西安站顺利落幕
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
[Niuke] [noip2015] jumping stone
省市区三级坐标边界数据csv转JSON
【案例分享】网络环路检测基本功能配置
Dynamic planning idea "from getting started to giving up"
boot - prometheus-push gateway 使用
随机推荐
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
Taro2.* 小程序配置分享微信朋友圈
「精致店主理人」青年创业孵化营·首期顺德场圆满结束!
Provincial and urban level three coordinate boundary data CSV to JSON
STM32开发资料链接分享
Implementation principle of waitgroup in golang
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
NEON优化:矩阵转置的指令优化案例
LLDP兼容CDP功能配置
迈动互联中标北京人寿保险,助推客户提升品牌价值
如何管理分布式团队?
Dynamic planning idea "from getting started to giving up"
Boot - Prometheus push gateway use
Let's see through the network i/o model from beginning to end
Install Firefox browser on raspberry pie /arm device
Rainstorm effect in levels - ue5
Realize incremental data synchronization between MySQL and ES
系统休眠文件可以删除吗 系统休眠文件怎么删除
Data type of pytorch tensor
NEON优化:性能优化经验总结