$chartId, "msgtype"=>"text", "text"=>[ "content"=>$message ], "safe"=>0 ]; return HttpCurl::postCurl($url,$data); } /** * 发送图片到企微 * @param string $imgPath 图片地址 * @param string $chartId 群聊ID */ public static function sendImgToBaoJing(string $imgPath,$chartId="") { //获取群聊ID if(!$chartId){ $chartId = env("QIWEI_BAOJING_CHART_ID"); } if(!$chartId){ throw new QiWeiException("请先配置企微群聊ID"); } $accessToken = self::getBaoJingAccessToken(); $res = self::uploadTmpSource($accessToken,$imgPath); $url = self::$baseUrl."appchat/send?access_token=".$accessToken; //组装报警信息 $data = [ "chatid"=>$chartId, "msgtype"=>"image", "image"=>[ "media_id"=>$res ], "safe"=>0 ]; return HttpCurl::postCurl($url,$data); } /** * @param string $imgPath * @param string $chartId * @return mixed * @throws QiWeiException */ public static function sendImgTxtToBaoJing($coverPath,$contentUrl,$desc,$title,$chartId="") { //获取群聊ID if(!$chartId){ $chartId = env("QIWEI_BAOJING_CHART_ID"); } if(!$chartId){ throw new QiWeiException("请先配置企微群聊ID"); } $accessToken = self::getBaoJingAccessToken(); $url = self::$baseUrl."appchat/send?access_token=".$accessToken; //组装报警信息 $data = [ "chatid"=>$chartId, "msgtype"=>"news", "news"=>[ "articles"=>[ [ "title"=>$title, "description"=>$desc, "url"=>$contentUrl, "picurl"=>$coverPath ] ] ], "safe"=>0 ]; return HttpCurl::postCurl($url,$data); } /** * 上传图片到临时素材 * @param string $accessToken token * @param string $filepath 图片地址 */ private static function uploadTmpSource(string $accessToken,$filepath) { $url = self::$baseUrl."media/upload?access_token=".$accessToken."&type=image"; $file = new \CURLFile($filepath,"application/octet-stream"); $data = [ "media"=>$file ]; $ch = curl_init($url); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); curl_setopt($ch,CURLOPT_HTTPHEADER,["multipart/form-data"]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_BINARYTRANSFER,true); $result = curl_exec($ch); if(curl_errno($ch)){ curl_close($ch); throw new HttpCustomeException("文件上传失败"); } curl_close($ch); $res = json_decode($result,1); if($res["status"]){ return $res; } throw new HttpCustomeException("文件上传失败"); } }