-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.class.php
More file actions
1172 lines (1105 loc) · 32.3 KB
/
Channel.class.php
File metadata and controls
1172 lines (1105 loc) · 32.3 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* 百度云消息通道服务 PHP SDK
*
* 本文件提供百度云消息通道服务的PHP版本SDK
*
* @author 百度移动.云事业部
* @copyright Copyright (c) 2012-2020 百度在线网络技术(北京)有限公司
* @version 2.0.0
* @package
*/
if ( ! defined ( 'API_ROOT_PATH' ) )
{
define ( 'API_ROOT_PATH', dirname( __FILE__));
}
require_once ( API_ROOT_PATH . '/lib/RequestCore.class.php' );
require_once ( API_ROOT_PATH . '/lib/ChannelException.class.php' );
require_once ( API_ROOT_PATH . '/lib/BaeBase.class.php' );
/**
*
* Channel
*
* Channel类提供百度云消息通道服务的PHP版本SDK,用户首先实例化这个类,设置自己的access_token,即可使用百度云消息通道服务
*
* @author 百度云消息通道服务@百度云架构部
*
* @version 1.0.0.0
*/
class Channel extends BaeBase
{
/**
* 可选参数的KEY
*
* 用户关注:是
* 在调用Channel类的SDK方法时,根据用户的个性化需要,可能需要传入可选参数,而可选参数需要放在关联数组$optional中传入,
* 这里定义了$optional数组可用的KEY
*/
/**
* 发起请求时的时间戳
*
* @var int TIMESTAMP
*/
const TIMESTAMP = 'timestamp';
/**
* 请求过期的时间
*
* 如果不填写,默认为10分钟
*
* @var int EXPIRES
*/
const EXPIRES = 'expires';
/**
* API版本号
*
* 用户一般不需要关注此项
*
* @var int VERSION
*/
const VERSION = 'v';
/**
* 消息通道ID号
*
* @var int CHANNEL_ID
*/
const CHANNEL_ID = 'channel_id';
/**
* 用户ID的类型
*
* 0:百度用户标识对称加密串;1:百度用户标识明文
*
* @var string USER_TYPE
*/
const USER_TYPE = 'user_type';
/**
* 设备类型
*
* 1:浏览器设备;2:PC设备;3:andorid设备
*
* @var int DEVICE_TYPE
*/
const DEVICE_TYPE = 'device_type';
/**
* 第几页
*
* 批量查询时,需要指定start,默认为第0页
*
* @var int START
*/
const START = 'start';
/**
* 每页多少条记录
*
* 批量查询时,需要指定limit,默认为100条
*
* @var int LIMIT
*/
const LIMIT = 'limit';
/**
* 消息ID json字符串
*
* @var string MSG_IDS
*/
const MSG_IDS = 'msg_ids';
const MSG_KEYS = 'msg_keys';
const IOS_MESSAGES = 'ios_messages';
const WP_MESSAGES = 'wp_messages';
/**
* 消息类型
*
* 扩展类型字段,0:默认类型
*
* @var int MESSAGE_TYPE
*/
const MESSAGE_TYPE = 'message_type';
/**
* 消息超时时间
*
* @var int MESSAGE_EXPIRES
*/
const MESSAGE_EXPIRES = 'message_expires';
/**
* 消息标签名称
*
* @var string TAG_NAME
*/
const TAG_NAME = 'tag';
/**
* 消息标签描述
*
* @var stirng TAG_INFO
*/
const TAG_INFO = 'info';
/**
* 消息标签id
*
* @var int TAG_ID
*/
const TAG_ID = 'tid';
/**
* 封禁时间
*
* @var int BANNED_TIME
*/
const BANNED_TIME = 'banned_time';
/**
* 回调域名
*
* @var string CALLBACK_DOMAIN
*/
const CALLBACK_DOMAIN = 'domain';
/**
* 回调uri
*
* @var string CALLBACK_URI
*/
const CALLBACK_URI = 'uri';
/**
* Channel常量
*
* 用户关注:否
*/
const APPID = 'appid';
const ACCESS_TOKEN = 'access_token';
const API_KEY = 'apikey';
const SECRET_KEY = 'secret_key';
const SIGN = 'sign';
const METHOD = 'method';
const HOST = 'host';
const USER_ID = 'user_id';
const MESSAGES = 'messages';
const PRODUCT = 'channel';
const DEFAULT_HOST = '10.23.248.79:8050';//'channel.api.duapp.com';
// const DEFAULT_HOST = 'localhost:1234';//'channel.api.duapp.com';
const NAME = "name";
const DESCRIPTION = "description";
const CERT = "cert";
const RELEASE_CERT = "release_cert";
const DEV_CERT = "dev_cert";
const PUSH_TYPE = 'push_type';
/**
* Channel私有变量
*
* 用户关注:否
*/
protected $_apiKey = NULL;
protected $_secretKey = NULL;
protected $_requestId = 0;
protected $_curlOpts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 5
);
const PUSH_TO_USER = 1;
const PUSH_TO_TAG = 2;
const PUSH_TO_ALL = 3;
const PUSH_TO_DEVICE = 4;
/**
* Channel 错误常量
*
* 用户关注:否
*/
const CHANNEL_SDK_SYS = 1;
const CHANNEL_SDK_INIT_FAIL = 2;
const CHANNEL_SDK_PARAM = 3;
const CHANNEL_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR = 4;
const CHANNEL_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR = 5;
/**
* 错误常量与错误字符串的映射
*
* 用户关注:否
*/
protected $_arrayErrorMap = array
(
'0' => 'php sdk error',
self::CHANNEL_SDK_SYS => 'php sdk error',
self::CHANNEL_SDK_INIT_FAIL => 'php sdk init error',
self::CHANNEL_SDK_PARAM => 'lack param',
self::CHANNEL_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR => 'http status is error, and the body returned is not a json string',
self::CHANNEL_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR => 'http status is ok, but the body returned is not a json string',
);
/**
* setAccessKey
*
* 用户关注:是
* 服务类方法, 设置Channel对象的accessKey属性,如果用户在创建Channel对象时已经通过参数设置了accessKey,这里的设置将会覆盖以前的设置
*
* @access public
* @param string $accessKey
* @return 成功:true,失败:false
*
* @version
*/
public function setAccessKey ( $accessKey )
{
$this->_resetErrorStatus ( );
try
{
if ( $this->_checkString ( $accessKey, 1, 64 ) )
{
$this->_apiKey = $accessKey;
}
else
{
throw new ChannelException ( "invaid accessKey ( ${accessKey} ), which must be a 1 - 64 length string", self::CHANNEL_SDK_INIT_FAIL );
}
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
return true;
}
/**
* setSecretKey
*
* 用户关注:是
* 服务类方法, 设置Channel对象的secretKey属性,如果用户在创建Channel对象时已经通过参数设置了secretKey,这里的设置将会覆盖以前的设置
*
* @access public
* @param string $secretKey
* @return 成功:true,失败:false
*
* @version
*/
public function setSecretKey ( $secretKey )
{
$this->_resetErrorStatus ( );
try
{
if ( $this->_checkString ( $secretKey, 1, 64 ) )
{
$this->_secretKey = $secretKey;
}
else
{
throw new ChannelException ( "invaid secretKey ( ${secretKey} ), which must be a 1 - 64 length string", self::CHANNEL_SDK_INIT_FAIL );
}
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
return true;
}
/**
* setCurlOpts
*
* 用户关注:是
* 服务类方法, 设置HTTP交互的OPTION,同PHP curl库的所有opt参数
*
* @access public
* @param array $arr_curlopt
* @return 成功:true,失败:false
* @throws BcmsException
*
* @version 1.2.0
*/
public function setCurlOpts($arr_curlOpts)
{
$this->_resetErrorStatus();
try {
if (is_array($arr_curlOpts)) {
$this->_curlOpts = array_merge($this->_curlOpts, $arr_curlOpts);
}
else {
throw new ChannelException( 'invalid param - arr_curlOpts is not an array ['
. print_r($arr_curlOpts, true) . ']',
self::CHANNEL_SDK_INIT_FAIL);
}
} catch (Exception $ex) {
$this->_channelExceptionHandler( $ex );
return false;
}
return true;
}
/**
* getRequestId
*
* 用户关注:是
* 服务类方法,获取上次调用的request_id,如果SDK本身错误,则直接返回0
*
* @access public
* @return 上次调用服务器返回的request_id
*
* @version 1.0.0.0
*/
public function getRequestId ( )
{
return $this->_requestId;
}
/**
* queryBindList
*
* 用户关注:是
*
* 供服务器端根据userId[、channelId]查询绑定信息
*
* @access public
* @param string $userId 用户ID号
* @param array $optional 可选参数,支持的可选参数包括:Channel::CHANNEL_ID、Channel::DEVICE_TYPE、Channel::START、Channel::LIMIT
* @return 成功:PHP数组;失败:false
*
* @version 1.0.0.0
*/
public function queryBindList ( $userId, $optional = NULL )
{
$this->_resetErrorStatus ( );
try
{
$tmpArgs = func_get_args ( );
$arrArgs = $this->_mergeArgs ( array ( self::USER_ID ), $tmpArgs );
$arrArgs [ self::METHOD ] = 'query_bindlist';
return $this->_commonProcess ( $arrArgs );
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
}
/**
* bindVerify
*
* 用户关注:是
*
* 校验userId[、channelId]是否已经绑定
*
* @access public
* @param string $userId 用户ID号
* @param array $optional 可选参数,支持的可选参数包括:Channel::CHANNEL_ID、Channel::DEVICE_TYPE
* @return 成功:PHP数组;失败:false
*
* @version 1.0.0.0
*/
public function verifyBind ( $userId, $optional = NULL )
{
$this->_resetErrorStatus ( );
try
{
$tmpArgs = func_get_args ( );
$arrArgs = $this->_mergeArgs ( array ( self::USER_ID ), $tmpArgs );
$arrArgs [ self::METHOD ] = 'verify_bind';
return $this->_commonProcess ( $arrArgs );
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
}
/**
* fetchMessage
*
* 用户关注:是
*
* 根据userId[、channelId]查询离线消息
*
* @access public
* @param string $userId 用户ID号
* @param array $optional 可选参数,支持的可选参数包括:Channel::CHANNEL_ID、Channel::START、Channel::LIMIT
* @return 成功:PHP数组;失败:false
*
* @version 1.0.0.0
*/
public function fetchMessage ( $userId, $optional = NULL )
{
$this->_resetErrorStatus ( );
try
{
$tmpArgs = func_get_args ( );
$arrArgs = $this->_mergeArgs ( array ( self::USER_ID ), $tmpArgs );
$arrArgs [ self::METHOD ] = 'fetch_msg';
return $this->_commonProcess ( $arrArgs );
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
}
/**
* messageCount
*
* 用户关注:是
*
* 根据userId[、channelId]查询离线消息的个数
*
* @access public
* @param string $userId 用户ID号
* @param array $optional 可选参数,支持的可选参数包括:Channel::CHANNEL_ID
* @return 成功:PHP数组;失败:false
*
* @version 1.0.0.0
*/
public function messageCount ( $userId, $optional = NULL )
{
$this->_resetErrorStatus ( );
try
{
$tmpArgs = func_get_args ( );
$arrArgs = $this->_mergeArgs ( array ( self::USER_ID ), $tmpArgs );
$arrArgs [ self::METHOD ] = 'fetch_msgcount';
return $this->_commonProcess ( $arrArgs );
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
}
/**
* deleteMessage
*
* 用户关注:是
*
* 根据userId、msgIds[、channelId]删除离线消息
*
* @access public
* @param string $userId 用户ID号
* @param string $msgIds 要删除哪些消息,如果是数组格式,则会自动做json_encode;
* @param array $optional 可选参数,支持的可选参数包括:Channel::CHANNEL_ID
* @return 成功:PHP数组;失败:false
*
* @version 1.0.0.0
*/
public function deleteMessage ( $userId, $msgIds, $optional = NULL )
{
$this->_resetErrorStatus ( );
try
{
$tmpArgs = func_get_args ( );
$arrArgs = $this->_mergeArgs ( array ( self::USER_ID, self::MSG_IDS ), $tmpArgs );
$arrArgs [ self::METHOD ] = 'delete_msg';
if(is_array($arrArgs [ self::MSG_IDS ])) {
$arrArgs [ self::MSG_IDS ] = json_encode($arrArgs [ self::MSG_IDS ]);
}
return $this->_commonProcess ( $arrArgs );
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
}
/**
* pushMessage
* 用户关注: 是
* 根据pushType, messages, message_type, [optinal] 推送消息
* @access public
* @param int $pushType 推送类型 取值范围 1-4, 1:单人,2:一群人tag, 3:所有人, 4:设备
* @param string $messages 要发送的消息,如果是数组格式,则会自动做json_encode;如果是json格式给出,必须与$msgIds对应起来;
* @param array $optional 可选参数,如果$pushType为单人,必须指定Channel::USER_ID(例:$optional[Channel::USER_ID] = 'xxx'),
* 如果$pushType为tag,必须指定Channel::TAG,
* 如果$pushType为设备,必须指定Channel::DEVICE, Channel::DEVICE取值范围1-5,1:浏览器,2:PC, 3:Andriod, 4:iOS, 5:Windos Phone
* 其他可选参数:Channel::MSG_KEYS 发送的消息key,如果是数组格式,则会自动做json_encode,必须与$messages对应起来;
* Channel::MESSAGE_TYPE 消息类型,取值范围 0-1, 0:消息(透传),1:通知,默认为0
* 还可指定Channel::MESSAGE_EXPIRES, Channel::MESSAGE_EXPIRES, Channel::CHANNLE_ID等
*
* @return 成功:PHP数组;失败:false
* @version 2.0.0.0
*/
public function pushMessage($pushType, $messages, $msgKeys, $optional = NULL)
{
$this->_resetErrorStatus();
try
{
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs (array(self::PUSH_TYPE , self::MESSAGES, self::MSG_KEYS), $tmpArgs);
$arrArgs[self::METHOD] = 'pushxmsg';
switch($pushType)
{
case self::PUSH_TO_USER:
if ( !array_key_exists(self::USER_ID, $arrArgs) || empty($arrArgs[self::USER_ID])){
throw new ChannelException("userId should be specified in optional[] when pushType is PUSH_TO_USER", self::CHANNEL_SDK_PARAM);
}
break;
case self::PUSH_TO_TAG:
if (!array_key_exists(self::TAG_NAME, $arrArgs) || empty($arrArgs[self::TAG_NAME])){
throw new ChannelException("tag should be specified in optional[] when pushType is PUSH_TO_TAG", self::CHANNEL_SDK_PARAM);
}
break;
case self::PUSH_TO_ALL:
break;
case self::PUSH_TO_DEVICE:
if (!array_key_exists(self::CHANNEL_ID, $arrArgs)){
throw new ChannelException("channelId should be specified in optional[] when pushType is PUSH_TO_DEVICE", self::CHANNEL_SDK_PARAM);
}
break;
default:
throw new ChannelException("pushType value is not supported or not specified", self::CHANNEL_SDK_PARAM);
}
$arrArgs[self::PUSH_TYPE] = $pushType;
if(is_array($arrArgs [ self::MESSAGES ])) {
$arrArgs [ self::MESSAGES ] = json_encode($arrArgs [ self::MESSAGES ]);
}
if(is_array($arrArgs [ self::MSG_KEYS ])) {
$arrArgs [ self::MSG_KEYS ] = json_encode($arrArgs [ self::MSG_KEYS ]);
}
return $this->_commonProcess ( $arrArgs );
}
catch (Exception $ex)
{
$this->_channelExceptionHandler( $ex );
return false;
}
}
/**
* createTag: 创建消息标签
*
* 用户关注: 是
*
* @access public
* @param string $tagName 标签名称
* @param array $optional 可选参数,支持的可选参数包括 self::TAG_INFO
* @return 成功: array; 失败: false
*
* @version 1.0.0.0
*/
public function createTag($tagName, $optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(array(self::TAG_NAME), $tmpArgs);
$arrArgs[self::METHOD] = 'create_tag';
return $this->_commonProcess($arrArgs);
} catch (Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* fetchTag: 查询消息标签信息
*
* 用户关注: 是
*
* @param int $tagId 标签ID号
* @param array $optional
* @return 成功:PHP数组;失败:false
*/
public function fetchTag($optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(null, $tmpArgs);
$arrArgs[self::METHOD] = 'fetch_tag';
return $this->_commonProcess($arrArgs);
} catch (Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* destroyTag: 删除消息标签
*
* 用户关注: 是
*
* @param int $tagId 消息标签ID号
* @param array $optional
* @return 成功:PHP数组;失败:false
*/
public function destroyTag($tagName, $optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(array(self::NAME), $tmpArgs);
$arrArgs[self::METHOD] = 'destroy_tag';
return $this->_commonProcess($arrArgs);
} catch (Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* queryUserTag: 查询用户相关的标签
*
* 用户关注: 是
*
* @param string $userId 用户ID号
* @param array $optional
* @return 成功:PHP数组;失败:false
*/
public function queryUserTags($userId, $optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(array(self::USER_ID), $tmpArgs);
$arrArgs[self::METHOD] = 'query_user_tags';
return $this->_commonProcess($arrArgs);
} catch (Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* initAppIoscert: 初始化应用ios证书
*
* 用户关注: 是
*
* @param string $name 证书名称
* @param string description 证书描述
* @param string $cert 证书内容
* @param array $optional
* @return 成功:PHP数组;失败:false
*/
public function initAppIoscert($name, $description, $release_cert, $dev_cert, $optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(array(self::NAME, self::DESCRIPTION, self::RELEASE_CERT, self::DEV_CERT), $tmpArgs);
$arrArgs[self::METHOD] = "init_app_ioscert";
return $this->_commonProcess($arrArgs);
} catch(Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* updateAppIoscert: 修改ios证书内容
*
* 用户关注: 是
*
* @param array $optional可选参数,支持的可选参数包括 self::NAME, self::DESCRIPTION, self::CERT
* @return 成功:PHP数组;失败:false
*/
public function updateAppIoscert($optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(array(), $tmpArgs);
$arrArgs[self::METHOD] = "update_app_ioscert";
return $this->_commonProcess($arrArgs);
} catch(Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* queryAppIoscert: 查询ios证书内容
*
* 用户关注: 是
*
* @param array $optional
* @return 成功:PHP数组;失败:false
*/
public function queryAppIoscert($optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(array(), $tmpArgs);
$arrArgs[self::METHOD] = "query_app_ioscert";
return $this->_commonProcess($arrArgs);
} catch(Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* destroyAppIoscert: 删除ios证书内容
*
* 用户关注: 是
*
* @param array $optional
* @return 成功:PHP数组;失败:false
*/
public function destroyAppIoscert($optional = null)
{
$this->_resetErrorStatus();
try {
$tmpArgs = func_get_args();
$arrArgs = $this->_mergeArgs(array(), $tmpArgs);
$arrArgs[self::METHOD] = "destroy_app_ioscert";
return $this->_commonProcess($arrArgs);
} catch(Exception $ex) {
$this->_channelExceptionHandler($ex);
return false;
}
}
/**
* queryDeviceType
*
* 用户关注:是
*
* 根据channelId查询设备类型
*
* @access public
* @param string $channelId 用户channel的ID号
* @return 成功:PHP数组;失败:false
*
* @version 1.0.0.0
*/
public function queryDeviceType ( $channelId, $optional = NULL )
{
$this->_resetErrorStatus ( );
try
{
$tmpArgs = func_get_args ( );
$arrArgs = $this->_mergeArgs ( array ( self::CHANNEL_ID ), $tmpArgs );
$arrArgs [ self::METHOD ] = 'query_device_type';
return $this->_commonProcess ( $arrArgs );
}
catch ( Exception $ex )
{
$this->_channelExceptionHandler ( $ex );
return false;
}
}
public function __construct ($accessKey = NULL, $secretKey = NULL, $arr_curlOpts = array())
{
$this->_apiKey = $accessKey;
$this->_secretKey = $secretKey;
if (!is_array($arr_curlOpts)) {
throw new ChannelException('invalid param - arr_curlopt is not an array ['
. print_r($arr_curlOpts, true) . ']',
self::CHANNEL_SDK_INIT_FAIL);
}
$this->_curlOpts = array_merge($this->_curlOpts, $arr_curlOpts);
$this->_resetErrorStatus();
}
/**
* _checkString
*
* 用户关注:否
*
* 检查参数是否是一个大于等于$min且小于等于$max的字符串
*
* @access protected
* @param string $str 要检查的字符串
* @param int $min 字符串最小长度
* @param int $max 字符串最大长度
* @return 成功:true;失败:false
*
* @version 1.0.0.0
*/
protected function _checkString($str, $min, $max)
{
if (is_string($str) && strlen($str) >= $min && strlen($str) <= $max) {
return true;
}
return false;
}
/**
* _getKey
*
* 用户关注:否
* 获取AK/SK/TOKEN/HOST的统一过程函数
*
* @access protected
* @param array $opt 参数数组
* @param string $opt_key 参数数组的key
* @param string $member 对象成员
* @param string $g_key 全局变量的名字
* @param string $env_key 环境变量的名字
* @param int $min 字符串最短值
* @param int $max 字符串最长值
* @throws ChannelException 如果出错,则抛出ChannelException异常,异常类型为self::CHANNEL_SDK_PARAM
*
* @version 1.0.0.0
*/
protected function _getKey(&$opt,
$opt_key,
$member,
$g_key,
$env_key,
$min,
$max,
$throw = true)
{
$dis = array(
'access_token' => 'access_token',
);
global $$g_key;
if (isset($opt[$opt_key])) {
if (!$this->_checkString($opt[$opt_key], $min, $max)) {
throw new ChannelException ( 'invalid ' . $dis[$opt_key] . ' in $optinal ('
. $opt[$opt_key] . '), which must be a ' . $min . '-' . $max
. ' length string', self::CHANNEL_SDK_PARAM );
}
return;
}
if ($this->_checkString($member, $min, $max)) {
$opt[$opt_key] = $member;
return;
}
if (isset($$g_key)) {
if (!$this->_checkString($$g_key, $min, $max)) {
throw new ChannelException('invalid ' . $g_key . ' in global area ('
. $$g_key . '), which must be a ' . $min . '-' . $max
. ' length string', self::CHANNEL_SDK_PARAM);
}
$opt[$opt_key] = $$g_key;
return;
}
if (false !== getenv($env_key)) {
if (!$this->_checkString(getenv($env_key), $min, $max)) {
throw new ChannelException( 'invalid ' . $env_key . ' in environment variable ('
. getenv($env_key) . '), which must be a ' . $min . '-' . $max
. ' length string', self::CHANNEL_SDK_PARAM);
}
$opt[$opt_key] = getenv($env_key) ;
return;
}
if ($opt_key === self::HOST) {
$opt[$opt_key] = self::DEFAULT_HOST;
return;
}
if ($throw) {
throw new ChannelException('no param (' . $dis[$opt_key] . ') was found',
self::CHANNEL_SDK_PARAM);
}
}
/**
* _adjustOpt
*
* 用户关注:否
*
* 参数调整方法
*
* @access protected
* @param array $opt 参数数组
* @throws ChannelException 如果出错,则抛出异常,异常号为 self::CHANNEL_SDK_PARAM
*
* @version 1.0.0.0
*/
protected function _adjustOpt(&$opt)
{
if (!isset($opt) || empty($opt) || !is_array($opt)) {
throw new ChannelException('no params are set',self::CHANNEL_SDK_PARAM);
}
if (!isset($opt[self::TIMESTAMP])) {
$opt[self::TIMESTAMP] = time();
}
$this->_getKey($opt, self::HOST, null, 'g_host',
'HTTP_BAE_ENV_ADDR_CHANNEL', 1, 1024);
$this->_getKey($opt, self::API_KEY, $this->_apiKey,
'g_apiKey', 'HTTP_BAE_ENV_AK', 1, 64, false);
//$opt[self::HOST] = self::DEFAULT_HOST;
//$opt[self::API_KEY] = $this->_apiKey;
if (isset($opt[self::SECRET_KEY])) {
unset($opt[self::SECRET_KEY]);
}
}
/**
* _genSign
*
*用户关注: 否
*
* 根据method, url, 参数内容 生成签名
*/
protected function _genSign($method, $url, $arrContent)
{
//$secret_key = $this->_secretKey;
$opt = array();
$this->_getKey($opt, self::SECRET_KEY, $this->_secretKey,
'g_secretKey', 'HTTP_BAE_ENV_SK', 1, 64, false);
$secret_key = $opt[self::SECRET_KEY];
$gather = $method.$url;
ksort($arrContent);
foreach($arrContent as $key => $value)
{
$gather .= $key.'='.$value;
}
$gather .= $secret_key;
$sign = md5(urlencode($gather));
return $sign;
}
/**
* _baseControl
*
* 用户关注:否
*
* 网络交互方法
*
* @access protected
* @param array $opt 参数数组
* @throws ChannelException 如果出错,则抛出异常,错误号为self::CHANNEL_SDK_SYS
*
* @version 1.0.0.0
*/
protected function _baseControl($opt)
{
$content = '';
$resource = 'channel';
if (isset($opt[self::CHANNEL_ID]) && !is_null($opt[self::CHANNEL_ID])) {
$resource = $opt[self::CHANNEL_ID];
unset($opt[self::CHANNEL_ID]);
}
$host = $opt[self::HOST];
unset($opt[self::HOST]);
$url = 'http://' . $host . '/rest/2.0/' . self::PRODUCT . '/';
$url .= $resource;
$http_method = 'POST';
$opt[self::SIGN] = $this->_genSign($http_method, $url, $opt);