php实现发送微信模板消息的方法
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了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 $aessToken;
function __construct($appid, $secrect)
{
$this->appid = $appid;
$this->secrect = $secrect;
$this->aessToken = $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_set($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_set($ch, CURLOPT_HEADER, 0); //设置header
curl_set($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_set($ch, CURLOPT_POST, 1); //post提交方式
curl_set($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_set($ch, CURLOPT_URL, $url);
curl_set($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)) {
$aess_token = S($appid);
} else {
$url = "https://api.weixin.qq./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);
$aess_token = $arr['aess_token'];
S($appid, $aess_token, 720);
}
return $aess_token;
}
/
发送自定义的模板消息
@param $touser
@param $template_id
@param $url
@param $data
@param string $color
@return bool
/
public function doSend($touser, $template_id, $url, $data, $color = '#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,
'color' => $color,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq./cgi-bin/message/template/send?aess_token=" . $this->aessToken;
$dataRes = $this->request_post($url, urldecode($json_template));
if ($dataRes['errcode'] == 0) {
return true;
} else {
return false;
}
}
}
namespace Org\Weixin;
/
Created by PhpStorm.
User: StandOpen
Date: 15-1-7
Time: 9:41
/
class OrderPush
{
protected $appid;
protected $secrect;
protected $aessToken;
function __construct($appid, $secrect)
{
$this->appid = $appid;
$this->secrect = $secrect;
$this->aessToken = $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_set($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_set($ch, CURLOPT_HEADER, 0); //设置header
curl_set($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_set($ch, CURLOPT_POST, 1); //post提交方式
curl_set($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_set($ch, CURLOPT_URL, $url);
curl_set($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)) {
$aess_token = S($appid);
} else {
$url = "https://api.weixin.qq./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);
$aess_token = $arr['aess_token'];
S($appid, $aess_token, 720);
}
return $aess_token;
}
/
发送自定义的模板消息
@param $touser
@param $template_id
@param $url
@param $data
@param string $color
@return bool
/
public function doSend($touser, $template_id, $url, $data, $color = '#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,
'color' => $color,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq./cgi-bin/message/template/send?aess_token=" . $this->aessToken;
$dataRes = $this->request_post($url, urldecode($json_template));
if ($dataRes['errcode'] == 0) {
return true;
} else {
return false;
}
}
}
希望本文所述对大家的php程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程