webhookUrl = $webhookUrl; } /** * 设置 Webhook URL * @param string $url * @return $this */ public function setWebhookUrl($url) { $this->webhookUrl = $url; return $this; } /** * 设置超时时间 * @param int $timeout * @return $this */ public function setTimeout($timeout) { $this->timeout = $timeout; return $this; } /** * 发送文本消息 * @param string $content 消息内容 * @param array $mentioned_list @成员的用户ID列表 * @param array $mentioned_mobile_list @成员的手机号列表 * @return array * @throws Exception */ public function sendText($content, $mentioned_list = [], $mentioned_mobile_list = []) { $data = [ 'msgtype' => 'text', 'text' => [ 'content' => $content, 'mentioned_list' => $mentioned_list, 'mentioned_mobile_list' => $mentioned_mobile_list ] ]; return $this->sendMessage($data); } /** * 发送 Markdown 消息 * @param string $content Markdown 内容 * @return array * @throws Exception */ public function sendMarkdown($content) { $data = [ 'msgtype' => 'markdown', 'markdown' => [ 'content' => $content ] ]; return $this->sendMessage($data); } /** * 发送链接消息 * @param string $title 标题 * @param string $description 描述 * @param string $url 链接地址 * @param string $picurl 图片URL(可选) * @return array * @throws Exception */ public function sendLink($title, $description, $url, $picurl = '') { $data = [ 'msgtype' => 'news', 'news' => [ 'articles' => [ [ 'title' => $title, 'description' => $description, 'url' => $url, 'picurl' => $picurl ] ] ] ]; return $this->sendMessage($data); } /** * 发送图片消息 * @param string $base64 图片内容的base64编码 * @param string $md5 图片内容的md5值 * @return array * @throws Exception */ public function sendImage($base64, $md5) { $data = [ 'msgtype' => 'image', 'image' => [ 'base64' => $base64, 'md5' => $md5 ] ]; return $this->sendMessage($data); } /** * 发送文件消息 * @param string $media_id 文件ID * @return array * @throws Exception */ public function sendFile($media_id) { $data = [ 'msgtype' => 'file', 'file' => [ 'media_id' => $media_id ] ]; return $this->sendMessage($data); } /** * 发送模板卡片消息 * @param array $template_card 模板卡片数据 * @return array * @throws Exception */ public function sendTemplateCard($template_card) { $data = [ 'msgtype' => 'template_card', 'template_card' => $template_card ]; return $this->sendMessage($data); } /** * 发送消息到企业微信 * @param array $data 消息数据 * @return array * @throws Exception */ private function sendMessage($data) { if (empty($this->webhookUrl)) { throw new Exception('Webhook URL 未设置'); } $jsonData = json_encode($data, JSON_UNESCAPED_UNICODE); $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $this->webhookUrl, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $jsonData, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Content-Length: ' . strlen($jsonData) ], CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { throw new Exception('请求失败: ' . $error); } if ($httpCode !== 200) { throw new Exception('HTTP 请求失败,状态码: ' . $httpCode); } $result = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception('响应数据解析失败: ' . json_last_error_msg()); } if (isset($result['errcode']) && $result['errcode'] !== 0) { throw new Exception('企业微信接口错误: ' . $result['errmsg'] . ' (错误码: ' . $result['errcode'] . ')'); } return $result; } /** * 快速发送文本消息的静态方法 * @param string $webhookUrl Webhook URL * @param string $content 消息内容 * @param array $mentioned_list @成员列表 * @param array $mentioned_mobile_list @手机号列表 * @return array * @throws Exception */ public static function quickSendText($webhookUrl, $content, $mentioned_list = [], $mentioned_mobile_list = []) { $webhook = new self($webhookUrl); return $webhook->sendText($content, $mentioned_list, $mentioned_mobile_list); } /** * 快速发送 Markdown 消息的静态方法 * @param string $webhookUrl Webhook URL * @param string $content Markdown 内容 * @return array * @throws Exception */ public static function quickSendMarkdown($webhookUrl, $content) { $webhook = new self($webhookUrl); return $webhook->sendMarkdown($content); } /** * 创建 Markdown 格式的消息内容 * @param string $title 标题 * @param array $items 内容项 * @param string $footer 底部内容 * @return string */ public static function createMarkdownContent($title, $items = [], $footer = '') { $content = "# {$title}\n\n"; foreach ($items as $item) { if (is_array($item)) { $content .= "- **{$item['label']}**: {$item['value']}\n"; } else { $content .= "- {$item}\n"; } } if (!empty($footer)) { $content .= "\n---\n{$footer}"; } return $content; } }