1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Broadcast.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/overtrue |
19
|
|
|
* @link http://overtrue.me |
20
|
|
|
*/ |
21
|
|
|
namespace EasyWeChat\Broadcast; |
22
|
|
|
|
23
|
|
|
use EasyWeChat\Core\AbstractAPI; |
24
|
|
|
use EasyWeChat\Core\Exceptions\HttpException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Broadcast. |
28
|
|
|
*/ |
29
|
|
|
class Broadcast extends AbstractAPI |
30
|
|
|
{ |
31
|
|
|
const API_SEND_BY_GROUP = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall'; |
32
|
|
|
const API_SEND_BY_OPENID = 'https://api.weixin.qq.com/cgi-bin/message/mass/send'; |
33
|
|
|
const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete'; |
34
|
|
|
const API_PREVIEW = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview'; |
35
|
|
|
const API_GET = 'http://api.weixin.qq.com/cgi-bin/message/mass/get'; |
36
|
|
|
|
37
|
|
|
const PREVIEW_BY_OPENID = 'touser'; |
38
|
|
|
const PREVIEW_BY_NAME = 'towxname'; |
39
|
|
|
|
40
|
|
|
const MSG_TYPE_TEXT = 'text'; // 文本 |
41
|
|
|
const MSG_TYPE_NEWS = 'news'; // 图文 |
42
|
|
|
const MSG_TYPE_VOICE = 'voice'; // 语音 |
43
|
|
|
const MSG_TYPE_IMAGE = 'image'; // 图片 |
44
|
|
|
const MSG_TYPE_VIDEO = 'video'; // 视频 |
45
|
|
|
const MSG_TYPE_CARD = 'card'; // 卡券 |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Send a message. |
49
|
|
|
* |
50
|
|
|
* @param string $msgType message type |
51
|
|
|
* @param mixed $message message |
52
|
|
|
* @param mixed $to |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
1 |
|
public function send($msgType, $message, $to = null) |
57
|
|
|
{ |
58
|
1 |
|
$message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->build(); |
59
|
|
|
|
60
|
1 |
|
$api = is_array($to) ? self::API_SEND_BY_OPENID : self::API_SEND_BY_GROUP; |
61
|
|
|
|
62
|
1 |
|
return $this->post($api, $message); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Send a text message. |
67
|
|
|
* |
68
|
|
|
* @param mixed $message message |
69
|
|
|
* @param mixed $to |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
1 |
|
public function sendText($message, $to = null) |
74
|
|
|
{ |
75
|
1 |
|
return $this->send(self::MSG_TYPE_TEXT, $message, $to); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Send a news message. |
80
|
|
|
* |
81
|
|
|
* @param mixed $message message |
82
|
|
|
* @param mixed $to |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
1 |
|
public function sendNews($message, $to = null) |
87
|
|
|
{ |
88
|
1 |
|
return $this->send(self::MSG_TYPE_NEWS, $message, $to); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Send a voice message. |
93
|
|
|
* |
94
|
|
|
* @param mixed $message message |
95
|
|
|
* @param mixed $to |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
1 |
|
public function sendVoice($message, $to = null) |
100
|
|
|
{ |
101
|
1 |
|
return $this->send(self::MSG_TYPE_VOICE, $message, $to); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Send a image message. |
106
|
|
|
* |
107
|
|
|
* @param mixed $message message |
108
|
|
|
* @param mixed $to |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
1 |
|
public function sendImage($message, $to = null) |
113
|
|
|
{ |
114
|
1 |
|
return $this->send(self::MSG_TYPE_IMAGE, $message, $to); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Send a video message. |
119
|
|
|
* |
120
|
|
|
* @param mixed $message message |
121
|
|
|
* @param mixed $to |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
1 |
|
public function sendVideo($message, $to = null) |
126
|
|
|
{ |
127
|
1 |
|
return $this->send(self::MSG_TYPE_VIDEO, $message, $to); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Send a card message. |
132
|
|
|
* |
133
|
|
|
* @param mixed $message message |
134
|
|
|
* @param mixed $to |
135
|
|
|
* |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
1 |
|
public function sendCard($message, $to = null) |
139
|
|
|
{ |
140
|
1 |
|
return $this->send(self::MSG_TYPE_CARD, $message, $to); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Preview a message. |
145
|
|
|
* |
146
|
|
|
* @param string $msgType message type |
147
|
|
|
* @param mixed $message message |
148
|
|
|
* @param string $to |
149
|
|
|
* @param string $by |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
1 |
|
public function preview($msgType, $message, $to, $by = self::PREVIEW_BY_OPENID) |
154
|
|
|
{ |
155
|
1 |
|
$message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->buildPreview($by); |
156
|
|
|
|
157
|
1 |
|
return $this->post(self::API_PREVIEW, $message); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Preview a text message. |
162
|
|
|
* |
163
|
|
|
* @param mixed $message message |
164
|
|
|
* @param string $to |
165
|
|
|
* @param string $by |
166
|
|
|
* |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
1 |
|
public function previewText($message, $to, $by = self::PREVIEW_BY_OPENID) |
170
|
|
|
{ |
171
|
1 |
|
return $this->preview(self::MSG_TYPE_TEXT, $message, $to, $by); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Preview a news message. |
176
|
|
|
* |
177
|
|
|
* @param mixed $message message |
178
|
|
|
* @param string $to |
179
|
|
|
* @param string $by |
180
|
|
|
* |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
1 |
|
public function previewNews($message, $to, $by = self::PREVIEW_BY_OPENID) |
184
|
|
|
{ |
185
|
1 |
|
return $this->preview(self::MSG_TYPE_NEWS, $message, $to, $by); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Preview a voice message. |
190
|
|
|
* |
191
|
|
|
* @param mixed $message message |
192
|
|
|
* @param string $to |
193
|
|
|
* @param string $by |
194
|
|
|
* |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
1 |
|
public function previewVoice($message, $to, $by = self::PREVIEW_BY_OPENID) |
198
|
|
|
{ |
199
|
1 |
|
return $this->preview(self::MSG_TYPE_VOICE, $message, $to, $by); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Preview a image message. |
204
|
|
|
* |
205
|
|
|
* @param mixed $message message |
206
|
|
|
* @param string $to |
207
|
|
|
* @param string $by |
208
|
|
|
* |
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
1 |
|
public function previewImage($message, $to, $by = self::PREVIEW_BY_OPENID) |
212
|
|
|
{ |
213
|
1 |
|
return $this->preview(self::MSG_TYPE_IMAGE, $message, $to, $by); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Preview a video message. |
218
|
|
|
* |
219
|
|
|
* @param mixed $message message |
220
|
|
|
* @param string $to |
221
|
|
|
* @param string $by |
222
|
|
|
* |
223
|
|
|
* @return mixed |
224
|
|
|
*/ |
225
|
1 |
|
public function previewVideo($message, $to, $by = self::PREVIEW_BY_OPENID) |
226
|
|
|
{ |
227
|
1 |
|
return $this->preview(self::MSG_TYPE_VIDEO, $message, $to, $by); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Preview a card message. |
232
|
|
|
* |
233
|
|
|
* @param mixed $message message |
234
|
|
|
* @param string $to |
235
|
|
|
* @param string $by |
236
|
|
|
* |
237
|
|
|
* @return mixed |
238
|
|
|
*/ |
239
|
1 |
|
public function previewCard($message, $to, $by = self::PREVIEW_BY_OPENID) |
240
|
|
|
{ |
241
|
1 |
|
return $this->preview(self::MSG_TYPE_CARD, $message, $to, $by); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Preview a message by name. |
246
|
|
|
* |
247
|
|
|
* @param string $msgType message type |
248
|
|
|
* @param mixed $message message |
249
|
|
|
* @param $to |
250
|
|
|
* |
251
|
|
|
* @return mixed |
252
|
|
|
*/ |
253
|
1 |
|
public function previewByName($msgType, $message, $to) |
254
|
|
|
{ |
255
|
1 |
|
return $this->preview($msgType, $message, $to, self::PREVIEW_BY_NAME); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Preview a text message by name. |
260
|
|
|
* |
261
|
|
|
* @param mixed $message message |
262
|
|
|
* @param $to |
263
|
|
|
* |
264
|
|
|
* @return mixed |
265
|
|
|
*/ |
266
|
1 |
|
public function previewTextByName($message, $to) |
267
|
|
|
{ |
268
|
1 |
|
return $this->preview(self::MSG_TYPE_TEXT, $message, $to, self::PREVIEW_BY_NAME); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Preview a news message by name. |
273
|
|
|
* |
274
|
|
|
* @param mixed $message message |
275
|
|
|
* @param $to |
276
|
|
|
* |
277
|
|
|
* @return mixed |
278
|
|
|
*/ |
279
|
1 |
|
public function previewNewsByName($message, $to) |
280
|
|
|
{ |
281
|
1 |
|
return $this->preview(self::MSG_TYPE_NEWS, $message, $to, self::PREVIEW_BY_NAME); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Preview a voice message by name. |
286
|
|
|
* |
287
|
|
|
* @param mixed $message message |
288
|
|
|
* @param $to |
289
|
|
|
* |
290
|
|
|
* @return mixed |
291
|
|
|
*/ |
292
|
1 |
|
public function previewVoiceByName($message, $to) |
293
|
|
|
{ |
294
|
1 |
|
return $this->preview(self::MSG_TYPE_VOICE, $message, $to, self::PREVIEW_BY_NAME); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Preview a image message by name. |
299
|
|
|
* |
300
|
|
|
* @param mixed $message message |
301
|
|
|
* @param $to |
302
|
|
|
* |
303
|
|
|
* @return mixed |
304
|
|
|
*/ |
305
|
1 |
|
public function previewImageByName($message, $to) |
306
|
|
|
{ |
307
|
1 |
|
return $this->preview(self::MSG_TYPE_IMAGE, $message, $to, self::PREVIEW_BY_NAME); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Preview a video message by name. |
312
|
|
|
* |
313
|
|
|
* @param mixed $message message |
314
|
|
|
* @param $to |
315
|
|
|
* |
316
|
|
|
* @return mixed |
317
|
|
|
*/ |
318
|
1 |
|
public function previewVideoByName($message, $to) |
319
|
|
|
{ |
320
|
1 |
|
return $this->preview(self::MSG_TYPE_VIDEO, $message, $to, self::PREVIEW_BY_NAME); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Preview a card message by name. |
325
|
|
|
* |
326
|
|
|
* @param mixed $message message |
327
|
|
|
* @param $to |
328
|
|
|
* |
329
|
|
|
* @return mixed |
330
|
|
|
*/ |
331
|
1 |
|
public function previewCardByName($message, $to) |
332
|
|
|
{ |
333
|
1 |
|
return $this->preview(self::MSG_TYPE_CARD, $message, $to, self::PREVIEW_BY_NAME); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Delete a broadcast. |
338
|
|
|
* |
339
|
|
|
* @param string $msgId |
340
|
|
|
* |
341
|
|
|
* @return bool |
342
|
|
|
*/ |
343
|
1 |
|
public function delete($msgId) |
344
|
|
|
{ |
345
|
|
|
$options = [ |
346
|
1 |
|
'msg_id' => $msgId, |
347
|
1 |
|
]; |
348
|
|
|
|
349
|
1 |
|
return $this->post(self::API_DELETE, $options); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get a broadcast status. |
354
|
|
|
* |
355
|
|
|
* @param string $msgId |
356
|
|
|
* |
357
|
|
|
* @return array |
358
|
|
|
*/ |
359
|
1 |
|
public function status($msgId) |
360
|
|
|
{ |
361
|
|
|
$options = [ |
362
|
1 |
|
'msg_id' => $msgId, |
363
|
1 |
|
]; |
364
|
|
|
|
365
|
1 |
|
return $this->post(self::API_GET, $options); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* post request. |
370
|
|
|
* |
371
|
|
|
* @param string $url |
372
|
|
|
* @param array|string $options |
373
|
|
|
* |
374
|
|
|
* @return array|bool |
375
|
|
|
* |
376
|
|
|
* @throws HttpException |
377
|
|
|
*/ |
378
|
4 |
|
private function post($url, $options) |
379
|
|
|
{ |
380
|
4 |
|
return $this->parseJSON('json', [$url, $options]); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|