You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-test/core/advancedbroadcast.lib.php

153 lines
7.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace LaneWeChat\Core; // 定义命名空间为 LaneWeChat\Core表示该类属于 LaneWeChat 模块的核心部分
/**
* 高级群发功能类
*
* 该类提供了多种高级群发功能包括上传图文消息、根据分组ID群发图文、文本、语音、图片和视频消息等。
*/
class AdvancedBroadcast {
/**
* 上传图文消息到微信服务器
*
* @param array $articles 图文消息数组,包含多篇文章的相关信息
* @return string|bool 返回上传成功后的media_id或在失败时返回false
*/
public static function uploadNews($articles) {
// 构建请求URL包含access_token使用AccessToken类的getAccessToken方法获取
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST'; // 请求方式为POST
// 遍历文章数组对每个文章的字段进行URL编码以确保特殊字符不会影响请求
foreach ($articles as &$article) {
$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; // 将编码后的文章数组赋值给模板的articles字段
$template = json_encode($template); // 将模板数组转换为JSON字符串
// 发起网络请求调用Curl类的方法传入URL、数据和请求方式
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
// 返回media_id如果结果中没有media_id则返回false
return empty($result['media_id']) ? false : $result['media_id'];
}
/**
* 根据分组ID群发图文消息
*
* @param int $groupId 分组ID
* @param string $mediaId 图文消息的media_id
* @param bool $isToAll 是否群发所有用户默认为false
* @return mixed 返回群发操作的结果
*/
public static function sentNewsByGroup($groupId, $mediaId, $isToAll = false) {
// 构建请求URL包含access_token
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' . AccessToken::getAccessToken();
$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群发文本消息
*
* @param int $groupId 分组ID
* @param string $content 文本内容
* @param bool $isToAll 是否群发所有用户默认为false
* @return mixed 返回群发操作的结果
*/
public static function sentTextByGroup($groupId, $content, $isToAll = false) {
// 构建请求URL包含access_token
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' . AccessToken::getAccessToken();
$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群发语音消息
*
* @param int $groupId 分组ID
* @param string $mediaId 语音消息的media_id
* @param bool $isToAll 是否群发所有用户默认为false
* @return mixed 返回群发操作的结果
*/
public static function sentVoiceByGroup($groupId, $mediaId, $isToAll = false) {
// 构建请求URL包含access_token
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' . AccessToken::getAccessToken();
$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群发图片消息
*
* @param int $groupId 分组ID
* @param string $mediaId 图片消息的media_id
* @param bool $isToAll 是否群发所有用户默认为false
* @return mixed 返回群发操作的结果
*/
public static function sentImageByGroup($groupId, $mediaId, $isToAll = false) {
// 构建请求URL包含access_token
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' . AccessToken::getAccessToken();
$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群发视频消息
*
* @param string $mediaId 视频消息的media_id
* @param string $title 视频标题
* @param string $description 视频描述
* @param int $groupId 分组ID
* @param bool $isToAll 是否群发所有用户默认为false
* @return bool 返回群发操作是否成功
*/
public static function sentVideoByGroup($mediaId, $title, $description, $groupId, $isToAll = false) {
// 构建请求URL包含access_token
$queryUrl = 'https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=' . AccessToken::getAccessToken();
$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;
}
// 如果视频上传成功,可以在这里添加发送视频消息的代码
}
}