Files
kiri-wchat/wchat/officialaccount/Material.php
T
2020-11-18 11:19:05 +08:00

251 lines
7.2 KiB
PHP

<?php
namespace wchat\officialaccount;
use Exception;
/**
* Class Material
* @package wchat\officialaccount
*/
class Material extends AfficialAccount
{
/**
* @param $filePath
* @param $type
* @return mixed
* @throws Exception
*/
public function addMedia($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/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();
}
}