|
|
|
@ -1,79 +1,91 @@
|
|
|
|
|
<?php
|
|
|
|
|
namespace LaneWeChat\Core;
|
|
|
|
|
namespace LaneWeChat\Core; // 定义命名空间,表明这个类属于LaneWeChat模块的核心部分
|
|
|
|
|
|
|
|
|
|
class AdvancedBroadcast{
|
|
|
|
|
class AdvancedBroadcast{ // 定义一个名为AdvancedBroadcast的类,用于高级群发功能
|
|
|
|
|
// 上传图文消息到微信服务器
|
|
|
|
|
public static function uploadNews($articles){
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token='.AccessToken::getAccessToken();
|
|
|
|
|
$queryAction = 'POST';
|
|
|
|
|
foreach($articles as &$article){
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=' AccessToken::getAccessToken(); // 构建请求URL,包含access_token
|
|
|
|
|
$queryAction = 'POST'; // 请求方式为POST
|
|
|
|
|
foreach($articles as &$article){ // 遍历文章数组,对每个文章的字段进行URL编码
|
|
|
|
|
$article['author'] = urlencode($article['author']);
|
|
|
|
|
$article['title'] = urlencode($article['title']);
|
|
|
|
|
$article['content'] = urlencode($article['content']);
|
|
|
|
|
$article['digest'] = urlencode($article['digest']);
|
|
|
|
|
}
|
|
|
|
|
$template = array();
|
|
|
|
|
$template['articles'] = $articles;
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
$template = urldecode($template);
|
|
|
|
|
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
|
|
|
|
|
return empty($result['media_id']) ? false : $result['media_id'];
|
|
|
|
|
$template = array(); // 创建模板数组
|
|
|
|
|
$template['articles'] = $articles; // 将编码后的文章数组赋值给模板的articles字段
|
|
|
|
|
$template = json_encode($template); // 将模板数组转换为JSON字符串
|
|
|
|
|
$template = urldecode($template); // 对JSON字符串进行URL解码(这一步可能是多余的,因为JSON字符串不需要URL解码)
|
|
|
|
|
$result = Curl::callWebServer($queryUrl, $template, $queryAction); // 发起网络请求,调用Curl类的方法
|
|
|
|
|
return empty($result['media_id']) ? false : $result['media_id']; // 返回media_id,如果结果中没有media_id则返回false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据分组ID群发图文消息
|
|
|
|
|
public static function sentNewsByGroup($groupId, $mediaId, $isToAll=false){
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
|
|
|
|
$queryAction = 'POST';
|
|
|
|
|
$template = array();
|
|
|
|
|
$template['filter']['group_id'] = $groupId;
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll;
|
|
|
|
|
$template['mpnews']['media_id'] = $mediaId;
|
|
|
|
|
$template['msgtype'] = 'mpnews';
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' AccessToken::getAccessToken(); // 构建请求URL
|
|
|
|
|
$queryAction = 'POST'; // 请求方式为POST
|
|
|
|
|
$template = array(); // 创建模板数组
|
|
|
|
|
$template['filter']['group_id'] = $groupId; // 设置分组ID
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll; // 设置是否群发所有用户
|
|
|
|
|
$template['mpnews']['media_id'] = $mediaId; // 设置图文消息的media_id
|
|
|
|
|
$template['msgtype'] = 'mpnews'; // 设置消息类型为图文消息
|
|
|
|
|
$template = json_encode($template); // 将模板数组转换为JSON字符串
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction); // 发起网络请求
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据分组ID群发文本消息
|
|
|
|
|
public static function sentTextByGroup($groupId, $content, $isToAll=false){
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
|
|
|
|
$queryAction = 'POST';
|
|
|
|
|
$template = array();
|
|
|
|
|
$template['filter']['group_id'] = $groupId;
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll;
|
|
|
|
|
$template['text']['content'] = $content;
|
|
|
|
|
$template['msgtype'] = 'text';
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' AccessToken::getAccessToken(); // 构建请求URL
|
|
|
|
|
$queryAction = 'POST'; // 请求方式为POST
|
|
|
|
|
$template = array(); // 创建模板数组
|
|
|
|
|
$template['filter']['group_id'] = $groupId; // 设置分组ID
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll; // 设置是否群发所有用户
|
|
|
|
|
$template['text']['content'] = $content; // 设置文本内容
|
|
|
|
|
$template['msgtype'] = 'text'; // 设置消息类型为文本消息
|
|
|
|
|
$template = json_encode($template); // 将模板数组转换为JSON字符串
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction); // 发起网络请求
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据分组ID群发语音消息
|
|
|
|
|
public static function sentVoiceByGroup($groupId, $mediaId, $isToAll=false){
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
|
|
|
|
$queryAction = 'POST';
|
|
|
|
|
$template = array();
|
|
|
|
|
$template['filter']['group_id'] = $groupId;
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll;
|
|
|
|
|
$template['voice']['media_id'] = $mediaId;
|
|
|
|
|
$template['msgtype'] = 'voice';
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' AccessToken::getAccessToken(); // 构建请求URL
|
|
|
|
|
$queryAction = 'POST'; // 请求方式为POST
|
|
|
|
|
$template = array(); // 创建模板数组
|
|
|
|
|
$template['filter']['group_id'] = $groupId; // 设置分组ID
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll; // 设置是否群发所有用户
|
|
|
|
|
$template['voice']['media_id'] = $mediaId; // 设置语音消息的media_id
|
|
|
|
|
$template['msgtype'] = 'voice'; // 设置消息类型为语音消息
|
|
|
|
|
$template = json_encode($template); // 将模板数组转换为JSON字符串
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction); // 发起网络请求
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据分组ID群发图片消息
|
|
|
|
|
public static function sentImageByGroup($groupId, $mediaId, $isToAll=false){
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
|
|
|
|
$queryAction = 'POST';
|
|
|
|
|
$template = array();
|
|
|
|
|
$template['filter']['group_id'] = $groupId;
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll;
|
|
|
|
|
$template['image']['media_id'] = $mediaId;
|
|
|
|
|
$template['msgtype'] = 'image';
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' AccessToken::getAccessToken(); // 构建请求URL
|
|
|
|
|
$queryAction = 'POST'; // 请求方式为POST
|
|
|
|
|
$template = array(); // 创建模板数组
|
|
|
|
|
$template['filter']['group_id'] = $groupId; // 设置分组ID
|
|
|
|
|
$template['filter']['is_to_all'] = $isToAll; // 设置是否群发所有用户
|
|
|
|
|
$template['image']['media_id'] = $mediaId; // 设置图片消息的media_id
|
|
|
|
|
$template['msgtype'] = 'image'; // 设置消息类型为图片消息
|
|
|
|
|
$template = json_encode($template); // 将模板数组转换为JSON字符串
|
|
|
|
|
return Curl::callWebServer($queryUrl, $template, $queryAction); // 发起网络请求
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据分组ID群发视频消息
|
|
|
|
|
public static function sentVideoByGroup($mediaId, $title, $description, $groupId, $isToAll=false){
|
|
|
|
|
$queryUrl = 'https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token='.AccessToken::getAccessToken();
|
|
|
|
|
$queryAction = 'POST';
|
|
|
|
|
$template = array();
|
|
|
|
|
$template['media_id'] = $mediaId;
|
|
|
|
|
$template['title'] = $title;
|
|
|
|
|
$template['description'] = $description;
|
|
|
|
|
$template = json_encode($template);
|
|
|
|
|
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
|
|
|
|
|
if(empty($result['type']) || $result['type'] != 'video' || empty($result['media_id'])){
|
|
|
|
|
$queryUrl = 'https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=' AccessToken::getAccessToken(); // 构建请求URL
|
|
|
|
|
$queryAction = 'POST'; // 请求方式为POST
|
|
|
|
|
$template = array(); // 创建模板数组
|
|
|
|
|
$template['media_id'] = $mediaId; // 设置视频消息的media_id
|
|
|
|
|
$template['title'] = $title; // 设置视频标题
|
|
|
|
|
$template['description'] = $description; // 设置视频描述
|
|
|
|
|
$template = json_encode($template); // 将模板数组转换为JSON字符串
|
|
|
|
|
$result = Curl::callWebServer($queryUrl, $template, $queryAction); // 发起网络请求
|
|
|
|
|
if(empty($result['type']) || $result['type'] != 'video' || empty($result['media_id'])){ // 检查返回结果是否符合预期
|
|
|
|
|
// 如果结果中没有type字段,或者type不是video,或者没有media_id,则返回false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// 如果视频上传成功,可以在这里添加发送视频消息的代码
|
|
|
|
|
}
|
|
|
|
|
}
|