微信模版消息推送
2018-01-24 20:10
232 查看
class WeixinPush { protected $appid; protected $secret; protected $accessToken; function __construct($appid, $secret) { if ($appid && $secret) { $this->appid = $appid; $this->secret = $secret; $this->accessToken = $this->getToken($appid, $secret); } } /** * 发送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; } /** * 获取token * @param $appid * @param $appsecret * @return mixed */ protected function getToken($appid, $appsecret) { $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']; return $access_token; } /** * 发送自定义的模板消息 * @param $touser * @param $template_id * @param $url * @param $data * @param string $topcolor * @return bool */ public function doSend($touser, $template_id, $url, $type, $topcolor = '#7B68EE') { $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)); print_r($dataRes); if ($dataRes['errcode'] == 0) { return true; } else { return false; } } } $data = array( 'first'=>array('value'=>urlencode("您的订单已提交成功"),'color'=>"#743A3A"), 'orderID'=>array('value'=>urlencode("123456789"),'color'=>"#743A3A"), 'orderMoneySum'=>array('value'=>urlencode("30.0"),'color'=>'#0000FF'), 'backupFieldName'=>array('value'=>urlencode("商品信息"),'color'=>'#0000FF'), 'backupFieldData'=>array('value'=>urlencode("商品名称"),'color'=>'#0000FF'), 'remark'=>array('value'=>urlencode('\\n如有问题请致电029-88888888或直接在微信留言,小菜将第一时间为您服务'),'color'=>'#000000'), ); $weixin = new WeixinPush($appid , $secret); $weixin->doSend($touser, $template_id, $url, $data);
$touser就是接受你消息那个人的OPENID
相关文章推荐
- 微信自定义推送模版消息
- 【模版消息】C#推送微信模版消息(Senparc.Weixin.MP.dll)
- 微信推送模版消息
- 【模版消息】C#推送微信模版消息(Senparc.Weixin.MP.dll)
- C# 微信接口之推送模版消息
- 微信开发(5):公众号消息与事件推送处理
- java推送微信模板消息
- 关于企业微信自定义消息推送过程
- 微信小程序消息推送配置Token校验失败问题
- 微信小程序统一服务消息下发接口 公众号和小程序消息都可以在一个接口推送了
- 微信开发——4、处理各种消息推送
- 微信的模板消息推送
- java推送微信模板消息
- 微信消息推送协议简单分析
- 微信推送消息--发送模板消息
- 使用Python制作自动推送微信消息提醒的备忘录功能
- 微信推送模板消息的PHP代码整理
- 突破微信小程序模板消息限制,实现无限制主动推送
- 微信服务号推送模板信息服务器验证与模板消息推送
- 飞鸽快信微信消息-LINUX服务器告警、状态监控预警、程序报警提醒等消息实时推送工具