2019-11-11 18:14:47 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
namespace wchat\officialaccount;
|
2019-11-11 18:14:47 +08:00
|
|
|
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
use wchat\common\HttpClient;
|
|
|
|
|
use wchat\common\Result;
|
2019-11-11 18:14:47 +08:00
|
|
|
|
|
|
|
|
class SourceMaterial extends AfficialAccount
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private $uploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/upload?';
|
|
|
|
|
private $getUploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/get';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $file
|
|
|
|
|
* @return bool|mixed|Result
|
|
|
|
|
*/
|
|
|
|
|
public function ImageUpload($file)
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
|
return $this->sendError('文件不存在.', 404);
|
|
|
|
|
}
|
|
|
|
|
return $this->upload($file, 'image');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $file
|
|
|
|
|
* @return bool|mixed|Result
|
|
|
|
|
*/
|
|
|
|
|
public function VoiceUpload($file)
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
|
return $this->sendError('文件不存在.', 404);
|
|
|
|
|
}
|
|
|
|
|
return $this->upload($file, 'voice');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $file
|
|
|
|
|
* @return bool|mixed|Result
|
|
|
|
|
*/
|
|
|
|
|
public function VideoUpload($file)
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
|
return $this->sendError('文件不存在.', 404);
|
|
|
|
|
}
|
|
|
|
|
return $this->upload($file, 'video');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $file
|
|
|
|
|
* @return bool|mixed|Result
|
|
|
|
|
*/
|
|
|
|
|
public function ThumbUpload($file)
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
|
return $this->sendError('文件不存在.', 404);
|
|
|
|
|
}
|
|
|
|
|
return $this->upload($file, 'thumb');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $media_id
|
|
|
|
|
* @return array|mixed|Result
|
|
|
|
|
*/
|
|
|
|
|
public function MediaGet($media_id)
|
|
|
|
|
{
|
|
|
|
|
$client = HttpClient::NewRequest();
|
|
|
|
|
|
|
|
|
|
$accessToken = $this->config->getAccessToken();
|
|
|
|
|
|
|
|
|
|
return $client->get($this->getUploadUrl, ['access_token' => $accessToken, 'media_id' => $media_id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $file
|
|
|
|
|
* @param $type
|
|
|
|
|
* @return bool|mixed
|
|
|
|
|
*/
|
|
|
|
|
private function upload($file, $type)
|
|
|
|
|
{
|
|
|
|
|
$uploadInfo['media'] = new \CURLFile($file);
|
|
|
|
|
$uploadInfo['form-data[filename]'] = $uploadInfo['media']->getFilename();
|
2020-05-22 15:17:24 +08:00
|
|
|
$uploadInfo['form-data[Content-Type]'] = $uploadInfo['media']->getMimeType();
|
2019-11-11 18:14:47 +08:00
|
|
|
|
|
|
|
|
$accessToken = $this->config->getAccessToken();
|
|
|
|
|
|
|
|
|
|
$url = $this->uploadUrl . 'access_token=' . $accessToken . '&type=' . $type;
|
|
|
|
|
$result = $this->request->post($url, $uploadInfo);
|
|
|
|
|
if (!$result->isResultsOK()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return $result->getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|