发送公众号模板消息
<?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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器