|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wechat\API; |
|
4
|
|
|
|
|
5
|
|
|
use Wechat\Api; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* 素材相关接口. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Tian. |
|
11
|
|
|
*/ |
|
12
|
|
|
class MaterialApi extends BaseApi |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* 获取素材列表. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $type 获取素材类型, |
|
18
|
|
|
* @param $type $offset 偏移 , |
|
|
|
|
|
|
19
|
|
|
* @param $type $count 数量 不能大于20 |
|
|
|
|
|
|
20
|
|
|
* |
|
21
|
|
|
* @return array. 注 返回值为图文(news)时 无法获得文章的正文 |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
|
|
public function batchget($type = 'image', $offset = 0, $count = 20) |
|
24
|
|
|
{ |
|
25
|
|
|
if (!is_string($type)) { |
|
26
|
|
|
$this->setError('参数错误,类型参数错误'); |
|
27
|
|
|
|
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$queryStr = [ |
|
32
|
|
|
"type" => $type, 'offset' => $offset, 'count' => $count, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
$res = $this->_post('batchget_material', $queryStr); |
|
36
|
|
|
|
|
37
|
|
|
return $res; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 上传图文素材. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $info 图文素材的信息, title 标题,thumb_media_id 封面图片ID,author 作者,digest 摘要,show_cover_pic 是否显示封面,content 内容支持html少于2万字符 小于1M,content_source_url 点击 阅读原文的url |
|
44
|
|
|
* |
|
45
|
|
|
* @return string. 成功返回mediaid |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function addNews($info) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
if (!is_array($info)) { |
|
50
|
|
|
$this->setError('参数错误'); |
|
51
|
|
|
|
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$data = ['articles' => $info]; |
|
56
|
|
|
|
|
57
|
|
|
$res = $this->_post('add_news', $data); |
|
58
|
|
|
|
|
59
|
|
|
return $res; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* 新增永久素材. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $file 媒体文件路径 |
|
66
|
|
|
* @param string $type 文件类型 (video,image,voice,thumb) |
|
67
|
|
|
* |
|
68
|
|
|
* @return array. 媒体文件ID 获取地址 https://mmbiz.qlogo.cn/mmbiz/NdxGKqW8jE9GbAqUEPdSgSvbUbProSmE8NbUFwIYnp0Duibs611ZsCLza6b2dS8Ex3CO5dtv0u1HP9QY32djCxA/0?wx_fmt=jpeg 视频素材 官方接口也无法上传 |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
|
|
public function add($file, $type = "image", $info = []) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$file || !$type) { |
|
73
|
|
|
$this->setError('参数缺失'); |
|
74
|
|
|
|
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!file_exists($file)) { |
|
79
|
|
|
$this->setError('文件路径不正确'); |
|
80
|
|
|
|
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($type == "video") { |
|
85
|
|
|
if (!is_array($info)) { |
|
86
|
|
|
$this->setError('视频信息必须是数组格式'); |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (empty($info['title']) || empty($info['introduction'])) { |
|
92
|
|
|
$this->setError('上传视频请填写标题介绍'); |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$description['title'] = $info['title']; |
|
|
|
|
|
|
98
|
|
|
$description['introduction'] = $info['introduction']; |
|
99
|
|
|
$des = json_encode($description); |
|
100
|
|
|
$data = ['media' => '@' . realpath($file), "type" => $type, "description" => $des]; |
|
101
|
|
|
} else { |
|
102
|
|
|
$data = ['media' => '@' . realpath($file), "type" => $type]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$node = 'add_material'; |
|
106
|
|
|
$res = $this->_post($node, $data, false); |
|
107
|
|
|
|
|
108
|
|
|
return $res; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* 获取 永久素材.通常为图文素材 其他素材可以在GetMaterialList获取对应地址 |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $media_id 素材ID |
|
115
|
|
|
* |
|
116
|
|
|
* @return array. |
|
|
|
|
|
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
public function get($media_id) |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
if (!is_string($media_id)) { |
|
121
|
|
|
$this->setError('参数错误,媒体ID参数错误'); |
|
122
|
|
|
|
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$queryStr = [ |
|
127
|
|
|
'media_id' => $media_id, |
|
128
|
|
|
]; |
|
129
|
|
|
|
|
130
|
|
|
$res = $this->_post('get_material', $queryStr); |
|
131
|
|
|
|
|
132
|
|
|
return $res; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* 删除永久素材. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $media_id 素材ID |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool. 成功返回ok |
|
|
|
|
|
|
141
|
|
|
*/ |
|
142
|
|
View Code Duplication |
public function del($media_id) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
if (!is_string($media_id)) { |
|
145
|
|
|
$this->setError('参数错误,媒体ID参数错误'); |
|
146
|
|
|
|
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$queryStr = [ |
|
151
|
|
|
"media_id" => $media_id, |
|
152
|
|
|
]; |
|
153
|
|
|
|
|
154
|
|
|
$res = $this->_post('del_material', $queryStr); |
|
155
|
|
|
|
|
156
|
|
|
return $res; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* 修改永久素材. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $media_id 素材ID,$info 保存信息,$index 多图文素材时才有意义 更新位置 |
|
163
|
|
|
* |
|
164
|
|
|
* @return bool. 成功返回ok |
|
|
|
|
|
|
165
|
|
|
*/ |
|
166
|
|
|
public function updateNews($media_id, $articles, $index = 0) |
|
167
|
|
|
{ |
|
168
|
|
|
if (!is_string($media_id)) { |
|
169
|
|
|
$this->setError('参数错误,媒体ID参数错误'); |
|
170
|
|
|
|
|
171
|
|
|
return false; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$queryStr = [ |
|
175
|
|
|
"media_id" => $media_id, |
|
176
|
|
|
"index" => intval($index), |
|
177
|
|
|
"articles" => $articles, |
|
178
|
|
|
]; |
|
179
|
|
|
|
|
180
|
|
|
$res = $this->_post('update_news', $queryStr); |
|
181
|
|
|
|
|
182
|
|
|
return $res; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* 获取素材总数. |
|
187
|
|
|
* |
|
188
|
|
|
* @return array. |
|
|
|
|
|
|
189
|
|
|
*/ |
|
190
|
|
|
public function getCount() |
|
191
|
|
|
{ |
|
192
|
|
|
$queryStr = []; |
|
193
|
|
|
|
|
194
|
|
|
$res = $this->_get('get_materialcount', $queryStr); |
|
195
|
|
|
|
|
196
|
|
|
return $res; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.