$user_chat_id, // 群主 ID 'Type' => $type, // 群组形态,包括 Public(陌生人社交群),Private(即 Work,好友工作群),ChatRoom(即 Meeting,会议群),AVChatRoom(直播群),Community(社群) 'GroupId' => $group_no, // 自定义群组 ID 'Name' => $room_name, 'Introduction' => json_encode([ '' ]),// 群简介 'Notification' => $notification,// 群公告 ]; $response = $this->post('/v4/group_open_http_svc/create_group', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('创建成功', $body); } /** * 创建群组 * @param string $user_chat_id * @param string $group_no * @param string $room_name * @param string $type * @param string $notification * @return bool */ public function destroy_group(string $group_no) { $data = [ 'GroupId' => $group_no, // 自定义群组 ID ]; $response = $this->post('/v4/group_open_http_svc/destroy_group', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('关闭成功', $body); } /** * 移除房间 * @param string $group_no * @param string $user_chat_id * @return bool */ public function delete_group_member(string $group_no,string $user_chat_id) { $data = [ 'GroupId' => $group_no, // 自定义群组 ID 'MemberToDel_Account' => [$user_chat_id] ]; $response = $this->post('/v4/group_open_http_svc/delete_group_member', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('操作成功', $body); } /** * 设置/取消直播群管理员 * @param string $group_no * @param string $user_chat_id * @return bool */ public function modify_admin(string $group_no,string $user_chat_id,int $type = 1) { $data = [ 'GroupId' => $group_no, // 自定义群组 ID 'CommandType' => $type, 'Admin_Account' => [$user_chat_id], ]; $response = $this->post('/v4/group_open_avchatroom_http_svc/modify_admin', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('操作成功', $body); } /** * 禁言 or 解除 * @param string $group_no * @param string $user_chat_id * @param $time // time = 0 解除 * @return bool */ public function forbid_send_msg(string $group_no,string $user_chat_id,int $time = 0) { $data = [ 'GroupId' => $group_no, // 自定义群组 ID 'Members_Account' => [$user_chat_id], 'MuteTime' => $time ]; $response = $this->post('/v4/group_open_http_svc/forbid_send_msg', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('操作成功', $body); } /** * 获取被禁言群成员列表 * @param string $group_no * @return bool */ public function get_group_muted_account(string $group_no) { $data = [ 'GroupId' => $group_no, // 自定义群组 ID ]; $response = $this->post('/v4/group_open_http_svc/get_group_muted_account', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('操作成功', $body); } /** * 获取被禁言群成员列表 * @param string $group_no * @return bool */ public function get_group_ban_member(string $group_no,int $limit = 100,int $Offset = 0) { $data = [ 'GroupId' => $group_no, // 自定义群组 ID 'Limit' => $limit, // 单次拉取限制,最大为100 'Offset' => $Offset, // 分页标识,首次传0,如果封禁的成员数大于100,下一次拉取需要设置为后台回复的 NextOffset ]; $response = $this->post('/v4/group_open_http_svc/get_group_ban_member', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('操作成功', $body); } /** * 封禁用户 * @param string $group_no * @param string $user_chat_id * @param $time // time = 0 解除 * @param $remark * @return bool */ public function ban_group_member(string $group_no,string $user_chat_id,int $time = 0, $remark = '违规操作') { $data = [ 'GroupId' => $group_no, 'Members_Account' => [$user_chat_id], 'Duration' => $time, // 封禁时长,单位:秒 'Description' => $remark// 封禁信息 ]; $response = $this->post('/v4/group_open_http_svc/ban_group_member', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('操作成功', $body); } /** * 解除封禁用户 * @param string $group_no * @param string $user_chat_id * @return bool */ public function unban_group_member(string $group_no,string $user_chat_id) { $data = [ 'GroupId' => $group_no, 'Members_Account' => [$user_chat_id] ]; $response = $this->post('/v4/group_open_http_svc/unban_group_member', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('操作成功', $body); } /** * 获取直播群在线人数 * @param int $room_no * @return bool */ public function get_online_member_num(string $room_no) { $data = [ 'GroupId' => (string)$room_no ]; $response = $this->post('/v4/group_open_http_svc/get_online_member_num', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('获取成功', $body); } /** * 设置计数器 * @param string $room_no * @param array $counter * @return bool */ public function update_group_counter(string $room_no, array $counter) { $data = [ 'GroupId' => $room_no,// 群组 ID 'GroupCounter' => $counter, 'Mode' => 'Set' ]; $response = $this->post('/v4/group_open_http_svc/update_group_counter', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('获取成功', $body); } /** * 设置群属性 * @param string $room_no * @param array $attr * @return bool */ public function modify_group_attr(string $room_no, array $attr) { $data = [ 'GroupId' => $room_no,// 群组 ID 'GroupAttr' => $attr ]; $response = $this->post('/v4/group_open_http_svc/modify_group_attr', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('获取成功', $body); } /** * 获取全部群组 * @return bool */ public function get_appid_group_list() { $data = [ 'Limit' => 1000,// 群主 ID 'Next' => 0 ]; $response = $this->post('/v4/group_open_http_svc/get_appid_group_list', $data); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') { return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []); } return $this->success('获取成功', $body); } /** * 获取直播推流信息 * @param string $stream_name 直播间号 * @param string $time 过期时间 * @return string */ public function getLivePushUrl(string $stream_name, string $time = '') { $key = $this->config['push_key']; $time = !empty($time) ? $time : date('Y-m-d H:i:s', strtotime('+1 day')); $domain = $this->config['push_domain']; if ($key && $time) { $txTime = strtoupper(base_convert((string)strtotime($time), 10, 16)); //txSecret = MD5( KEY + streamName + txTime ) $txSecret = md5($key . $stream_name . $txTime); $ext_str = "?" . http_build_query(["txSecret" => $txSecret, "txTime" => $txTime]); } return "rtmp://{$domain}/live/{$stream_name}" . ($ext_str ?? ""); } /** * 获取直播播放地址 * @param string $stream_name 您用来区别不同推流地址的唯一流名称 * @return string */ public function getLivePlayUrl(string $stream_name) { return "rtmp://{$this->config['play_domain']}/live/".$stream_name; } /** * 获取usersig签名-具体操作 */ public function userSig($user_id) { // 获取配置信息 $userSigObj = new GetUserSig($this->config["appid"], $this->config["key"]); return $userSigObj->genUserSig($user_id); } private function post(string $uri, array $params = []) { $random = rand(10000000, 99999999); $userSig = $this->usersig($this->config['identifier']); return $this->postJson("{$uri}?sdkappid={$this->config['appid']}&identifier={$this->config['identifier']}&usersig={$userSig}&random={$random}&contenttype=json", $params); } public function setConfig(array $config): TencentIm { $this->config = $config; return $this; } }