- 登录微信公众平台
点击左侧模板消息,在从模版库中添加找到类似的模板点详情后添加。
- 若没有找到能用的,可自己创建(预计审核7~14天)
- 以下TP5代码
class Message extends Base
{
public function index()
{
$openIdList = $this->getOpenId();//此方法可以获取已关注的openid列表,如需群发用些方法获取
$openid = 'XXXXXXXXXXXXXX';
$data = [
'touser' => $openid,
'template_id' => 'XXXXXXXXX_Ba5jYrMpm242CHg',//些处为公众平台添加的模板ID
'url' => $web_url = "http://www.baidu.com",//此处为推送消息“查看详情”的地址
'topcolor' => "#FF0000",
'data' => array(
'first' => array('value' => "公开课内容更新", 'color' => "#fc0101"),//类似副标题
'keyword1' => array('value' => '平台优势', 'color' => "#173177"),//标题
'keyword2' => array('value' => '具备互联网科技公司专业研...', 'color' => "#173177"),//内容
)
];
$get_all_access_token = $this->get_all_access_token();
$json_data = json_encode($data);//转化成json数组让微信可以接收
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $get_all_access_token;//模板消息请求URL
$res = $this->curl_post($url, urldecode($json_data));//请求开始
$res = json_decode($res, true);
if ($res['errcode'] == 0 && $res['errmsg'] == "ok") {
return json(['st' => 1, 'msg' => '发送成功']);
} else {
return json(['st' => 0, 'msg' => '发送失败']);
}
}
public function get_all_access_token()
{
$appid = 'wx66666XXX66666';//公众号appid
$secret = '2222222XXXXXX222221';//公众号密钥
$res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret);
$resdata = json_decode($res, true);
$access_token = $resdata['access_token'];
return $access_token;
}
/**
* Describe:获取关注公众号的用户
* User: yeungz
* Date: 2020/5/27 14:38
*/
public function getOpenId()
{
$appid = 'wx66666XXX66666';//公众号appid
$secret = '2222222XXXXXX222221';//公众号密钥
$res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret);
$resdata = json_decode($res, true);
$access_token = $resdata['access_token'];
$res1 = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" . $access_token);
$resdata1 = json_decode($res1, true);
$openidList = $resdata1['data']['openid'];
return $openidList;
}
/**
* 发起GET请求
* @param string $url 请求地址
* @param array $data 请求参数
* @param string $cookiePath 请求所保存的cookie路径
*/
function curl_get($url, $data = [], $cookiePath = '')
{
if (count($data) != 0) {
$url = get_query_url($url, $data);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
/**
* 发起POST请求
* @param string $url 请求地址
* @param string $data 请求参数
* @param string string $cookiePth
*/
function curl_post($url, $data = [], $cookiePath = '')
{
$ch = curl_init(); // 初始化
curl_setopt($ch, CURLOPT_URL, $url); // 抓取指定网页
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); // POST提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 请求参数
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath); // 连接结束后保存cookie信息的文件
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath); // 包含cookie信息的文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 禁用后cURL将终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 检查服务器SSL证书中是否存在一个公用名
$res = curl_exec($ch); // 执行一个cURL会话
curl_close($ch); // 关闭一个cURL会话
return $res;
}
}