forked from zemzheng/WeChatPHP-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeChatClient.php
417 lines (355 loc) · 17.3 KB
/
WeChatClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?PHP
/**
* @author zemzheng@gmail.clom
* @description
* @see https://github.com/zemzheng/WeChatPHP-SDK
* @see http://admin.wechat.com/wiki
* @see http://mp.weixin.qq.com/wiki
*/
class WeChatClient{
// SETTING
// Tail this file N U will see.
public static $_URL_API_ROOT;
public static $_URL_FILE_API_ROOT;
public static $_QRCODE_TICKET_DEFAULT_ID = 1;
public static $ERRCODE_MAP;
public static $_USERINFO_LANG;
// DATA
private $_appid;
private $_appsecret;
private static $_accessTokenCache = array();
private static $ERROR_LOGS = array();
public function __construct( $appid, $appsecret ){
$this->_appid = $appid;
$this->_appsecret = $appsecret;
}
public static function checkIsSuc( $res ){
$result = true;
if( is_string( $res ) ){
$res = json_decode( $res, true );
}
if( isset($res['errcode']) && ( 0 !== (int)$res['errcode']) ){
array_push( self::$ERROR_LOGS, $res );
$result = false;
}
return $result;
}
/**
* @method get
* @static
* @param {string}
* @return {string|boolen}
*/
public static function get( $url ){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
# curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if(!curl_exec($ch)){
error_log( curl_error ( $ch ));
$data = '';
} else {
$data = curl_multi_getcontent($ch);
}
curl_close($ch);
return $data;
}
/**
* @method post
* @static
* @param {string} $url URL to post data to
* @param {string|array} $data Data to be post
* @return {string|boolen} Response string or false for failure.
*/
private static function post( $url, $data ){
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
# curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
$data = curl_exec($ch);
if(!$data) error_log( curl_error ( $ch ) );
curl_close( $ch );
return $data;
}
public function getAccessToken( $tokenOnly = 1 ){
$myToeknInfo = null;
$appid = $this->_appid;
$appsecret = $this->_appsecret;
// check cache
if( isset( self::$_accessTokenCache[ $appid ] ) ){
$myToeknInfo = self::$_accessTokenCache[ $appid ];
if( time() < $myToeknInfo[ 'expire' ] ){
return $tokenOnly ? $myToeknInfo['token'] : $myToeknInfo;
}
}
// get new token
$url = self::$_URL_API_ROOT;
$url = "$url/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$json = self::get( $url );
$res = json_decode( $json, true );
if ( self::checkIsSuc($res) ){
// update cache
self::$_accessTokenCache[ $appid ] = $myToeknInfo = array(
'token' => $res['access_token'],
'expire' => time() + (int) $res['expires_in']
);
}
return $tokenOnly ? $myToeknInfo['token'] : $myToeknInfo;
}
public function setAccessToken( $tokenInfo ){
if( $tokenInfo ){
$appid = $this->_appid;
self::$_accessTokenCache[ $appid ] = array(
'token' => $tokenInfo['token'],
'expire' => $tokenInfo['expire']
);
}
}
// *************** media file upload/download ************
public function upload( $type, $file_path, $mediaidOnly = 1 ){
$access_token = $this->getAccessToken();
$url = self::$_URL_FILE_API_ROOT . "/cgi-bin/media/upload?access_token=$access_token&type=$type";
$res = self::post( $url, array( 'media' => "@$file_path" ) );
$res = json_decode( $res, true );
if( self::checkIsSuc( $res ) ){
return $mediaidOnly ? $res['media_id'] : $res;
}
return null;
}
public function download( $mid ){
$access_token = $this->getAccessToken();
$url = self::$_URL_FILE_API_ROOT . "/cgi-bin/media/get?access_token=$access_token&media_id=$mid";
return self::get( $url );
}
// *************** MENU ******************
public function getMenu(){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/menu/get?access_token=$access_token";
$json = self::get($url);
$res = json_decode($json, true);
if( self::checkIsSuc( $res ) ){
return $res;
}
return null;
}
public function deleteMenu(){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/menu/delete?access_token=$access_token";
$res = self::get($url);
return self::checkIsSuc( $res );
}
public function setMenu( $myMenu ){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/menu/create?access_token=$access_token";
if( defined( 'JSON_UNESCAPED_UNICODE' ) ){
$json = is_string( $myMenu ) ? $myMenu : json_encode( $myMenu, JSON_UNESCAPED_UNICODE );
} else{
$json = is_string( $myMenu ) ? $myMenu : json_encode( $myMenu );
}
$res = self::post($url, $json);
return self::checkIsSuc( $res );
}
// *************** send msg ******************
private function _send( $to, $type, $data ){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/message/custom/send?access_token=$access_token";
$json = json_encode(
array(
'touser' => $to,
'msgtype' => $type,
$type => $data
)
);
$res = self::post($url, $json);
return self::checkIsSuc( $res );
}
public function sendTextMsg( $to, $msg ){
return $this->_send( $to, 'text', array( 'content' => $msg ) );
}
public function sendImgMsg( $to, $mid ){
return $this->_send( $to, 'image', array( 'media_id' => $mid ) );
}
public function sendVoice( $to, $mid ){
return $this->_send( $to, 'voice', array( 'media_id' => $mid ) );
}
public function sendVideo( $to, $mid, $title, $desc ){
return $this->_send( $to, 'video', array(
'media_id' => $mid,
'title' => $title,
'description' => $desc
) );
}
public function sendMusic( $to, $url, $thumb_mid, $title, $desc = '', $hq_url = '' ){
return $this->_send( $to, 'music', array(
'media_id' => $mid,
'title' => $title,
'description' => $desc || $title,
'musicurl' => $url,
'thumb_media_id' => $thumb_mid,
'hqmusicurl' => $hq_url || $url
) );
}
static private function _filterForRichMsg( $articles ){
$i = 0;
$ii = len( $articles );
$list = array( 'title', 'desc', 'url', 'thumb_url' );
$result = array();
while( $i < $ii ){
$currentArticle = $articles[ $i++ ];
try{
array_push( $result, array(
'title' => $currentArticle['title'],
'description' => $currentArticle['desc'],
'url' => $currentArticle['url'],
'picurl' => $currentArticle['thumb_url']
) );
} catch( Exception $e ){}
}
return $result;
}
public function sendRichMsg( $to, $articles ){
return $this->_send( $to, 'news', array(
'articles' => self::_filterForRichMsg( $articles )
) );
}
// *************** followers admin ******************
// follower group
public function createGroup( $name ){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/create?access_token=$access_token";
$res = self::post( $url, json_encode( array(
'group' => array( 'name' => $name )
) ) );
$res = json_decode( $res, true );
return self::checkIsSuc( $res ) ? $res['group']['id'] : null;
}
public function renameGroup( $gid, $name ){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/update?access_token=$access_token";
$res = self::post( $url, json_encode( array(
'group' => array(
'id' => $gid,
'name' => $name
)
) ) );
$res = json_decode( $res, true );
return self::checkIsSuc( $res );
}
public function moveUserById( $uid, $gid ){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/members/update?access_token=$access_token";
$res = self::post(
$url,
json_encode(
array(
'openid' => $mid,
'to_groupid' => $gid
)
)
);
$res = json_decode( $res, true );
return self::checkIsSuc( $res );
}
public function getAllGroups(){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/get?access_token=$access_token";
$res = self::get( $url );
echo $res;
$res = json_decode( $res, true );
return self::checkIsSuc( $res ) ? $res['groups'] : null;
}
public function getGroupidByUserid( $uid ){
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/getid?access_token=$access_token";
$res = self::post( $url, json_encode( array(
'openid' => $mid
) ) );
$res = json_decode( $res, true );
return self::checkIsSuc( $res ) ? $res['groupid'] : null;
}
// *************** Followers info ******************
public function getUserInfoById( $uid, $lang='' ){
if( !$lang ) $lang = self::$_USERINFO_LANG;
$access_token = $this->getAccessToken();
$url = self::$_URL_API_ROOT . "/cgi-bin/user/info?access_token=$access_token&openid=$uid&lang=$lang";
$res = json_decode( self::get( $url ), true );
return self::checkIsSuc( $res ) ? $res : null;
}
public function getFollowersList( $next_id = '' ){
$access_token = $this->getAccessToken();
$extend = '';
if( $next_id ){
$extend = "&next_openid=$next_id";
}
$url = self::$_URL_API_ROOT . "/cgi-bin/user/get?access_token=${access_token}$extend";
$res = json_decode(
self::get( $url ),
true
);
return self::checkIsSuc( $res )
? array(
'total' => $res['total'],
'list' => $res['data']['openid'],
'next_id' => isset( $res['next_openid'] ) ? $res['next_openid'] : null
)
: null;
}
// ************************** qr code *****************
public static function getQrcodeImgByTicket( $ticket ){
return self::get( $this->getQrcodeImgUrlByTicket( $ticket ) );
}
public static function getQrcodeImgUrlByTicket( $ticket ){
$ticket = urlencode( $ticket );
return self::$_URL_API_ROOT . "/cgi-bin/showqrcode?ticket=$ticket";
}
public function getQrcodeTicket( $options = array() ){
$access_token = $this->getAccessToken();
$scene_id = isset( $options[ 'scene_id' ] ) ? (int)$options[ 'scene_id' ] : 0;
$expire = isset( $options[ 'expire' ] ) ? (int)$options[ 'expire' ] : 0;
$ticketOnly = isset( $options[ 'ticketOnly' ] ) ? $options[ 'ticketOnly' ] : 1;
if( $scene_id < 1 || $scene_id > 100000 ){
$scene_id = self::$_QRCODE_TICKET_DEFAULT_ID;
}
$url = "$_URL_API_ROOT/cgi-bin/qrcode/create?access_token=$access_token";
$data = array(
'action_name' => 'QR_LIMIT_SCENE',
'action_info' => array(
'scene' => array(
'scene_id' => $scene_id
)
)
);
if( $expire ){
$data['expire_seconds'] = $expire;
$data['action_name'] = 'QR_SCENE';
}
$data = json_encode( $data );
$res = self::post( $url, $data );
$res = json_decode( $res, true );
if( self::checkIsSuc( $res ) ){
return $ticketOnly ? $res['ticket'] : array(
'ticket' => $res['ticket'],
'expire' => $res['expire_seconds']
);
}
return null;
}
}
# ######################################################################
# admin.wechat.com
# ######################################################################
WeChatClient::$_URL_API_ROOT = 'https://api.wechat.com';
WeChatClient::$_URL_FILE_API_ROOT = 'http://file.api.weixin.qq.com';
WeChatClient::$_USERINFO_LANG = 'en';
# @see http://admin.wechat.com/wiki/index.php?title=Return_Codes
WeChatClient::$ERRCODE_MAP = array(
'-1' => 'System busy', '0' => 'Request succeeded', '40001' => 'Verification failed', '40002' => 'Invalid certificate type', '40003' => 'Invalid Open ID', '40004' => 'Invalid media file type', '40005' => 'Invalid file type', '40006' => 'Invalid file size', '40007' => 'Invalid media file ID', '40008' => 'Invalid message type', '40009' => 'Invalid image file size', '40010' => 'Invalid audio file size', '40011' => 'Invalid video file size', '40012' => 'Invalid thumbnail file size', '40013' => 'Invalid App ID', '40014' => 'Invalid access token', '40015' => 'Invalid menu type', '40016' => 'Invalid button quantity', '40017' => 'Invalid button quantity', '40018' => 'Invalid button name length', '40019' => 'Invalid button KEY length', '40020' => 'Invalid button URL length', '40021' => 'Invalid menu version', '40022' => 'Invalid sub-menu levels', '40023' => 'Invalid sub-menu button quantity', '40024' => 'Invalid sub-menu button type', '40025' => 'Invalid sub-menu button name length', '40026' => 'Invalid sub-menu button KEY length', '40027' => 'Invalid sub-menu button URL length', '40028' => 'Invalid custom menu user', '40029' => 'Invalid oauth code', '40030' => 'Invalid refresh token', '40031' => 'Invalid openid list', '40032' => 'Invalid openid list length', '40033' => 'Invalid request characters: The character "\uxxxx" cannot be included.', '40035' => 'Invalid parameters', '40038' => 'Invalid request format', '40039' => 'Invalid URL length', '40050' => 'Invalid group ID', '40051' => 'Invalid group name', '41001' => 'Parameter missing: access token', '41002' => 'Parameter missing: appid', '41003' => 'Parameter missing: refresh token', '41004' => 'Parameter missing: secret', '41005' => 'Multimedia file data missing', '41006' => 'Parameter missing: media id', '41007' => 'Sub-menu data missing', '41008' => 'Parameter missing: oauth code', '41009' => 'Parameter missing: openid', '42001' => 'access token timed out', '42002' => 'refresh token timed out', '42003' => 'oauth code timed out', '43001' => 'GET request required', '43002' => 'POST request required', '43003' => 'HTTPS request required', '43004' => 'The other user is not yet a follower', '43005' => 'The other user is not yet a follower', '44001' => 'Multimedia file is empty', '44002' => 'POST package is empty', '44003' => 'Rich media message is empty', '44004' => 'Text message is empty', '45001' => 'Error source: multimedia file size', '45002' => 'Message contents too long', '45003' => 'Title too long', '45004' => 'Description too long', '45005' => 'URL too long', '45006' => 'Image URL too long', '45007' => 'Audio play time over limit', '45008' => 'Rich media messages over limit', '45009' => 'Error source: interface call', '45010' => 'Message quantity over limit', '45015' => 'Response too late', '45016' => 'System group cannot be changed.',
'45017' => 'System name too long', '45018' => 'Too many groups', '46001' => 'Media data missing', '46002' => 'This menu version doesn\'t exist.', '46003' => 'This menu data doesn\'t exist.', '46004' => 'This user doesn\'t exist.', '47001' => 'Error while extracting JSON/XML contents', '48001' => 'Unauthorized API function', '50001' => 'The user is not authorized for this API'
);
# ######################################################################