php实现发送微信模板消息的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-05-15 22:03:35 浏览: 评论:0
这篇文章主要介绍了php实现发送微信模板消息的方法,实例分析了php操作curl及自定义模板消息的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php实现发送微信模板消息的方法。分享给大家供大家参考。具体如下:
该方法基于thinkphp实现实现,具体OrderPush.class.php文件如下:
- <?php
- namespace Org\Weixin;
- /**
- * Created by PhpStorm.
- * User: StandOpen
- * Date: 15-1-7
- * Time: 9:41
- */
- class OrderPush
- {
- protected $appid;
- protected $secrect;
- protected $accessToken;
- function __construct($appid, $secrect)
- {
- $this->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 (emptyempty($url) || emptyempty($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 (emptyempty($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;
- }
- }
- }
Tags: php发送微信模板
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)