博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 实现发送微信模板消息(转)
阅读量:7293 次
发布时间:2019-06-30

本文共 3028 字,大约阅读时间需要 10 分钟。

appid = $appid; $this->secrect = $secrect; $this->accessToken = $this->getToken($appid, $secrect); } /** * 发送post请求 * @param string $url * @param string $param * @return bool|mixed */ function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0); //设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1); //post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch); //运行curl curl_close($ch); return $data; } /** * 发送get请求 * @param string $url * @return bool|mixed */ function request_get($url = '') { if (empty($url)) { return false; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); curl_close($ch); return $data; } /** * @param $appid * @param $appsecret * @return mixed * 获取token */ protected function getToken($appid, $appsecret) { if (S($appid)) { $access_token = S($appid); } else { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret; $token = $this->request_get($url); $token = json_decode(stripslashes($token)); $arr = json_decode(json_encode($token), true); $access_token = $arr['access_token']; S($appid, $access_token, 720); } return $access_token; } /** * 发送自定义的模板消息 * @param $touser * @param $template_id * @param $url * @param $data * @param string $topcolor * @return bool */ public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE') { /* * data=>array( 'first'=>array('value'=>urlencode("您好,您已购买成功"),'color'=>"#743A3A"), 'name'=>array('value'=>urlencode("商品信息:微时代电影票"),'color'=>'#EEEEEE'), 'remark'=>array('value'=>urlencode('永久有效!密码为:1231313'),'color'=>'#FFFFFF'), ) */ $template = array( 'touser' => $touser, 'template_id' => $template_id, 'url' => $url, 'topcolor' => $topcolor, 'data' => $data ); $json_template = json_encode($template); $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken; $dataRes = $this->request_post($url, urldecode($json_template)); if ($dataRes['errcode'] == 0) { return true; } else { return false; } }}

 

转载于:https://www.cnblogs.com/xingmeng/p/5020219.html

你可能感兴趣的文章
Github常见错误
查看>>
板子集合
查看>>
第四十一课、编辑交互功能的实现------------------狄泰软件学院
查看>>
cocos2d-x之监听手机的物理按键
查看>>
python数据处理excel和pdf,并打包成exe
查看>>
基于 HTML5 WebGL 的低碳工业园区监控系统
查看>>
如何使绝对定位内部元素不继承父级宽度,而是靠内容自动撑开宽度(转载)
查看>>
《程序猿的生命周期》阅读有感
查看>>
重温排序算法
查看>>
Instrumentation 功能介绍(javaagent)
查看>>
Core J2EE Patterns - Data Access Object
查看>>
SpringCloud学习成长之路 六 cloud配置中心
查看>>
MyEclipse定位class文件
查看>>
STM32(HY-SRF05)超声波测距项目
查看>>
《practical Java》读书笔记
查看>>
数据库字段顺序的【坑】
查看>>
spring5新响应式框架-webflux实战
查看>>
软甲架构笔记 三
查看>>
STL training (uva上一些比较好的用来熟悉STL)
查看>>
[未完成]关于CSS的总结
查看>>