码迷,mamicode.com
首页 > 其他好文 > 详细

发送公众号模板消息

时间:2019-09-02 17:00:09      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:证书认证   min   exce   head   enc   expires   std   public   stat   

<?php
class wxservice
{
/**
* @todo w微信服务号接口地址
*/
private static $appid = "---------";
private static $appsecret = "----------------";
private static $template_id = [
"1"=>"==========",//服务完成提醒
"2"=>"============",//派单成功通知
];
private static $token;

public function __construct (){
self::gettoken();
}

/**
* @todo 获取access_token
*/
public static function gettoken(){
if(!file_exists("./upload/token.txt")){
fopen("./upload/token.txt", "w+");
}
$file = file_get_contents("./upload/token.txt");
$file = json_decode($file,true);
if( $file[‘date‘]<time() ){

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::$appid."&secret=".self::$appsecret;
$file = file_get_contents($url);
$file = json_decode($file,true);
$file[‘date‘] = time() + $file[‘expires_in‘] - 1200;
$fp = file_put_contents("./upload/token.txt", json_encode($file));
}
self::$token = $file[‘access_token‘];
}

public static function send_service($openid,$info = [],$type = 1){
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".self::$token;
$arr = [
"touser"=>$openid,
"template_id"=>self::$template_id[$type],
"url"=>"",
"miniprogram"=>"",
"data"=>[
"first"=>[
"value"=>"{$info[‘first‘]}",
"color"=>"#173177"
],
"keyword1"=>[
"value"=>"{$info[‘keyword1‘]}",
"color"=>"#173177"
],
"keyword2"=>[
"value"=>"{$info[‘keyword2‘]}",
"color"=>"#173177"
],
"keyword3"=>[
"value"=>"{$info[‘keyword3‘]}",
"color"=>"#173177"
],
"remark"=>[
"value"=>"{$info[‘remark‘]}",
"color"=>"#173177"
],
]
];
$msg = self::postcurl($url,json_encode($arr));
return $msg;

}

private static function postcurl($submitUrl,$postData = null)
{
//提交菜单
$curl = curl_init($submitUrl);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
curl_setopt($curl,CURLOPT_POST,true); // post传输数据
curl_setopt($curl,CURLOPT_POSTFIELDS,$postData);// post传输数据
$responseText = curl_exec($curl);
if($responseText == false)
{
throw new IException(curl_error($curl));
}
curl_close($curl);
return json_decode($responseText,true);
}
}

发送公众号模板消息

标签:证书认证   min   exce   head   enc   expires   std   public   stat   

原文地址:https://www.cnblogs.com/zlking/p/11447266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!