msgData['touser'] = $openid; } /** * @param string $content * @return Result * @throws Exception */ public function sendTextNews(string $content) { $this->msgData['msgtype'] = 'text'; $this->msgData['text'] = ['content' => $content]; return $this->sendKefuMsg(); } /** * @param $media_id * @return Result * @throws Exception */ public function sendImageNews(string $media_id) { $this->msgData['msgtype'] = 'image'; $this->msgData['image'] = ['media_id' => $media_id]; return $this->sendKefuMsg(); } /** * @param $media_id * @return Result * @throws Exception */ public function sendVoiceNews(string $media_id) { $this->msgData['msgtype'] = 'voice'; $this->msgData['voice'] = ['media_id' => $media_id]; return $this->sendKefuMsg(); } /** * @param $media_id * @return Result * @throws Exception */ public function sendMpNewsNews(string $media_id) { $this->msgData['msgtype'] = 'mpnews'; $this->msgData['mpnews'] = ['media_id' => $media_id]; return $this->sendKefuMsg(); } /** * @param string $title * @param string $description * @param string $url * @param string $picurl * @return Result * @throws Exception */ public function sendNewsNews(string $title, string $description, string $url, string $picurl) { $this->msgData['msgtype'] = 'news'; $this->msgData['news'] = ['articles' => [[ 'title' => $title, 'description' => $description, 'url' => $url, 'picurl' => $picurl ]]]; return $this->sendKefuMsg(); } /** * @param string $title * @return Result * @throws Exception */ public function sendCardNews(string $title) { $this->msgData['msgtype'] = 'wxcard'; $this->msgData['wxcard'] = ['card_id' => $title]; return $this->sendKefuMsg(); } /** * @param string $media_id * @param string $thumb_media_id * @param string $title * @param string $description * @return Result * @throws Exception */ public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description) { $this->msgData['msgtype'] = 'video'; $this->msgData['video'] = ['media_id' => [ 'media_id' => $media_id, 'thumb_media_id' => $thumb_media_id, 'title' => $title, 'description' => $description ]]; return $this->sendKefuMsg(); } /** * @param string $musicurl * @param string $hqmusicurl * @param string $thumb_media_id * @param string $title * @param string $description * @return Result * @throws Exception */ public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description) { $this->msgData['msgtype'] = 'music'; $this->msgData['music'] = [ 'title' => $title, 'description' => $description, 'musicurl' => $musicurl, 'hqmusicurl' => $hqmusicurl, 'thumb_media_id' => $thumb_media_id ]; return $this->sendKefuMsg(); } /** * @param string $head_content * @param string $tail_content * @param array $menus * @return Result * @throws Exception */ public function sendMenuNews(string $head_content, string $tail_content, array $menus = []) { $this->msgData['msgtype'] = 'msgmenu'; $this->msgData['msgmenu'] = [ 'head_content' => $head_content, 'tail_content' => $tail_content, ]; if (empty($menus) || !is_array($menus) || count($menus) < 2) { throw new Exception('菜单选项必须有2个'); } foreach ($menus as $key => $val) { $this->addNewsMenu($val['id'], $val['name']); } return $this->sendKefuMsg(); } private int $index = 0; /** * @param $id * @param $menuName * @return $this */ public function addNewsMenu($id, $menuName) { $lists['id'] = $id; $lists['content'] = $menuName; $this->msgData['msgmenu']['list'][$this->index] = $lists; ++$this->index; return $this; } /** * @param $title * @param $appid * @param $pagepath * @param $thumb_media_id * @return Result * @throws Exception */ public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id) { $this->msgData['msgtype'] = 'msgmenu'; $this->msgData['miniprogrampage'] = [ 'title' => $title, 'appid' => $appid, 'pagepath' => $pagepath, 'thumb_media_id' => $thumb_media_id, ]; return $this->sendKefuMsg(); } /** * @param string $filePath * @param string $type * @param bool $isPermanent * @param string $title * @param string $introduction * @return mixed * @throws Exception */ public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '') { if (!file_exists($filePath)) { throw new Exception('文件不存在'); } if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) { throw new Exception('暂不支持的文件类型'); } $token = $this->getAccessToken(); if ($isPermanent) { $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}"; } else { $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}"; } $mime = mime_content_type($filePath); $real_path = new \CURLFile(realpath($filePath)); $data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime); if ($isPermanent && $mime == 'video/mp3') { $data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction]; } $this->request->setMethod(HttpClient::POST); /** @var Result $body */ $data = $this->request->post($url, $data); if (!$data->isResultsOK()) { throw new Exception($data->getMessage()); } return $data->getData(); } /** * @return array */ public function getContents() { return $this->msgData; } /** * @return Result * @throws Exception */ private function sendKefuMsg() { $data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE); $url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken(); $this->request->setMethod(HttpClient::POST); $this->request->setErrorField('errcode'); $this->request->setErrorMsgField('errmsg'); $this->request->setHost('api.weixin.qq.com'); $this->request->addHeader('Content-Type', 'application/json'); /** @var Result $body */ $body = $this->request->post($url, $data); if (!$body->isResultsOK()) { throw new Exception($body->getMessage()); } return $body; } }