diff --git a/wchat/common/Help.php b/wchat/common/Help.php index fa6e9df..bb1a840 100644 --- a/wchat/common/Help.php +++ b/wchat/common/Help.php @@ -15,6 +15,10 @@ class Help extends Miniprogarampage { $xml = ""; foreach ($data as $key => $val) { + if (is_array($val)) { + $xml .= static::xmlChild($val); + continue; + } if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . ""; } else { @@ -26,6 +30,29 @@ class Help extends Miniprogarampage } + /** + * @param array $array + * @return string + */ + private static function xmlChild(array $array) + { + $string = ''; + foreach ($array as $key => $value) { + if (is_array($value)) { + $string .= static::xmlChild($value); + continue; + } + + if (is_numeric($value)) { + $string .= "<" . $key . ">" . $value . ""; + } else { + $string .= "<" . $key . ">"; + } + } + return $string; + } + + /** * @param $xml * @return mixed diff --git a/wchat/officialaccount/Material.php b/wchat/officialaccount/Material.php new file mode 100644 index 0000000..61bd3cf --- /dev/null +++ b/wchat/officialaccount/Material.php @@ -0,0 +1,250 @@ + $curlFile, + 'form-data[filename]' => $curlFile->getFilename(), + 'form-data[content-type]' => $curlFile->getMimeType() + ]; + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->upload('/cgi-bin/media/upload?access_token=' . $this->config->getAccessToken() . '&type=' . $type, $data); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * @param $filePath + * @param $type + * @return mixed + * @throws Exception + */ + public function addMaterial($filePath, $type) + { + $curlFile = new \CURLFile(realpath($filePath)); + $data = [ + "media" => $curlFile, + 'form-data[filename]' => $curlFile->getFilename(), + 'form-data[content-type]' => $curlFile->getMimeType() + ]; + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->upload('/cgi-bin/material/add_material?access_token=' . $this->config->getAccessToken() . '&type=' . $type, $data); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * @param array $articles + * @return mixed + * @throws Exception + */ + public function addNewsMaterial(array $articles) + { + $array['articles'] = []; + foreach ($articles as $article) { + $array['articles'][] = [ + 'title' => $article['title'], + 'thumb_media_id' => $article['thumb_media_id'], + 'author' => $article['author'], + 'digest' => $article['digest'], + 'show_cover_pic' => $article['show_cover_pic'], + 'content' => $article['content'], + 'content_source_url' => $article['content_source_url'], + 'need_open_comment' => $article['need_open_comment'], + 'only_fans_can_comment' => $article['only_fans_can_comment'] + + ]; + } + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->post('/cgi-bin/material/add_news?access_token=' . $this->config->getAccessToken(), $array); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * @param $filePath + * @param $type + * @return mixed + * @throws Exception + */ + public function addNewsMaterialImage($filePath, $type) + { + $curlFile = new \CURLFile(realpath($filePath)); + $data = [ + "media" => $curlFile, + 'form-data[filename]' => $curlFile->getFilename(), + 'form-data[content-type]' => $curlFile->getMimeType() + ]; + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->upload('/cgi-bin/material/add_material?access_token=' . $this->config->getAccessToken() . '&type=' . $type, $data); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * declare + * @param $media_id + * @return mixed + * @throws Exception + */ + public function getMaterial(string $media_id) + { + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->post('/cgi-bin/material/get_material?access_token=' . $this->config->getAccessToken(), [ + 'media_id' => $media_id + ]); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * declare + * @param $media_id + * @return mixed + * @throws Exception + */ + public function delMaterial(string $media_id) + { + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->post('/cgi-bin/material/del_material?access_token=' . $this->config->getAccessToken(), [ + 'media_id' => $media_id + ]); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * declare + * @param string $media_id + * @param int $index + * @param array $articles + * @return mixed + * @throws Exception + */ + public function modifyNewsMaterial(string $media_id, int $index, array $articles) + { + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->post('/cgi-bin/material/del_material?access_token=' . $this->config->getAccessToken(), [ + 'media_id' => $media_id, + 'index' => $index, + 'articles' => [ + 'title' => $articles['title'], + 'thumb_media_id' => $articles['thumb_media_id'], + 'author' => $articles['author'], + 'digest' => $articles['digest'], + 'show_cover_pic' => $articles['show_cover_pic'], + 'content' => $articles['content'], + 'content_source_url' => $articles['content_source_url'] + ] + ]); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * declare + * @return mixed + * @throws Exception + */ + public function getMaterialCount() + { + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->get('/cgi-bin/material/get_materialcount?access_token=' . $this->config->getAccessToken()); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } + + + /** + * declare + * @param int $offset + * @param int $size + * @param string $type + * @return mixed + * @throws Exception + */ + public function getMaterialList(int $offset, int $size, string $type) + { + $this->request->setHost('api.weixin.qq.com'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->addHeader('Content-Type', 'application/json'); + $data = $this->request->post('/cgi-bin/material/batchget_material?access_token=' . $this->config->getAccessToken(), [ + 'type' => $type, + 'offset' => $offset, + 'count' => $size + ]); + if (!$data->isResultsOK()) { + throw new Exception($data->getMessage()); + } + return $data->getData(); + } +}