PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ThinkPHP/Extend/Library/COM/BAIDU/BCMS/Bcms.class.php

https://github.com/xiaozhujun/houseRent
PHP | 1525 lines | 828 code | 52 blank | 645 comment | 87 complexity | 9439e7c9c44dbad4ca6394fef108010a MD5 | raw file
  1. <?php
  2. /**
  3. * 百度云消息服务 PHP SDK
  4. *
  5. * 本文件提供百度云消息服务的PHP版本SDK
  6. *
  7. * @author 百度移动.云事业部
  8. * @copyright Copyright (c) 2012-2020 百度在线网络技术(北京)有限公司
  9. * @version 1.2.0
  10. * @package
  11. */
  12. if ( ! defined ( 'API_ROOT_PATH' ) )
  13. {
  14. define ( 'API_ROOT_PATH', dirname( __FILE__));
  15. }
  16. //require_once ( API_ROOT_PATH . '/lib/RequestCore.class.php' );
  17. //require_once ( API_ROOT_PATH . '/lib/BcmsException.class.php' );
  18. //require_once ( API_ROOT_PATH . '/lib/BaeBase.class.php' );
  19. import ( "COM.BAIDU.BCMS.RequestCore" );
  20. import ( "COM.BAIDU.BCMS.BcmsException" );
  21. import ( "COM.BAIDU.BCMS.BaeBase" );
  22. /**
  23. *
  24. * Bcms
  25. *
  26. * Bcms类提供百度云消息服务的PHP版本SDK,用户首先实例化这个类,设置自己的accessKey、secretKey,即可使用百度云消息服务
  27. *
  28. * @author 百度消息服务@百度云架构部
  29. *
  30. * @version 1.2.0
  31. */
  32. class Bcms extends BaeBase
  33. {
  34. /**
  35. * 可选参数的KEY
  36. *
  37. * 用户关注:是
  38. * 在调用Bcms类的SDK方法时,根据用户的个性化需要,可能需要传入可选参数,而可选参数需要放在关联数组$optional中传入,
  39. * 这里定义了$optional数组可用的KEY
  40. * 如,在调用dropQueue方法时,指定过期时间为5分钟,则这样使用SDK:
  41. * $bcms = new Bcms ( $accessKey, $secretKey, $host );
  42. * $optional [ Bcms::EXPIRES ] = 5 * 60;
  43. * $bcms->dropQueue ( $queueName, $optional );
  44. */
  45. /**
  46. * 队列类型
  47. *
  48. * createQueue创建队列时,用户可以指定要创建的队列类型,如果不指定,BCMS默认创建单模式队列, 0: 多模式,1: 单模式
  49. * @var string QUEUE_ALIAS_NAME
  50. */
  51. const QUEUE_TYPE = 'queue_type';
  52. /**
  53. * 队列别名
  54. *
  55. * createQueue创建队列时,用户可以为队列指定一个别名
  56. * @var string QUEUE_ALIAS_NAME
  57. */
  58. const QUEUE_ALIAS_NAME = 'queue_alias_name';
  59. /**
  60. * 邮件发件人
  61. *
  62. * 在调用mail方法发送邮件时,用户可能需要指定发件人邮箱
  63. * @var string FROM
  64. */
  65. const FROM = 'from';
  66. /**
  67. * 生效起始时间
  68. *
  69. * 调用Grant赋予其它用户权限时,用户可能希望指定权限生效的时间
  70. * @var string EFFECT_START
  71. */
  72. const EFFECT_START = 'effect_start';
  73. /**
  74. * 生效结束时间
  75. *
  76. * 调用Grant赋予其它用户权限时,用户可能希望指定权限生效的时间
  77. * @var string EFFECT_END
  78. */
  79. const EFFECT_END = 'effect_end';
  80. /**
  81. * 消息ID号
  82. *
  83. * fetchMessage等,用户可能需要指定消息ID号
  84. * @var int MSG_ID
  85. */
  86. const MSG_ID = 'msg_id';
  87. /**
  88. * 要获取的消息个数
  89. *
  90. * fetch消息时,用户可能需要指定要获取多少个消息
  91. * @var int FETCH_NUM
  92. */
  93. const FETCH_NUM = 'fetch_num';
  94. /**
  95. * 邮件标题
  96. *
  97. * mail发送邮件时,用户可能需要指定邮件的标题
  98. * @var unknown_type
  99. */
  100. const MAIL_SUBJECT = 'mail_subject';
  101. /**
  102. * 请求发起时的时间戳
  103. *
  104. * 用户一般不需要设置,SDK会自动设置为当前时间
  105. * @var int TIMESTAMP
  106. */
  107. const TIMESTAMP = 'timestamp';
  108. /**
  109. * 请求过期时间
  110. *
  111. * 如果用户不设置,则默认10分钟后该请求过期
  112. * @var int EXPIRES
  113. */
  114. const EXPIRES = 'expires';
  115. /**
  116. * API版本号
  117. *
  118. * 用户一般不需要设置
  119. * @var string VERSION
  120. */
  121. const VERSION = 'v';
  122. /**
  123. * Bcms常量
  124. *
  125. * 用户关注:否
  126. */
  127. const QUEUE_NAME = 'queue_name';
  128. const MSG_TIMEOUT = 'msg_timeout';
  129. const DESTINATION = 'destination';
  130. const METHOD = 'method';
  131. const HOST = 'host';
  132. const PRODUCT = 'bcms';
  133. const SIGN = 'sign';
  134. const ACCESS_TOKEN = 'access_token';
  135. const SECRET_KEY = 'client_secret';
  136. const ACCESS_KEY = 'client_id';
  137. const ADDRESS = 'address';
  138. const MESSAGE = 'message';
  139. const LABEL = 'label';
  140. const USER = 'user';
  141. const USERTYPE = 'usertype';
  142. const ACTIONS = 'actions';
  143. const TOKEN = 'token';
  144. const DEFAULT_HOST = 'bcms.api.duapp.com';
  145. /**
  146. * Bcms私有变量
  147. *
  148. * 用户关注:否
  149. */
  150. private $_clientId = NULL;
  151. private $_clientSecret = NULL;
  152. private $_host = NULL;
  153. private $_requestId = 0;
  154. private $_curlOpts = array ( CURLOPT_TIMEOUT => 30, CURLOPT_CONNECTTIMEOUT => 5 );
  155. /**
  156. * Bcms 错误常量
  157. *
  158. * 用户关注:否
  159. */
  160. const BCMS_SDK_SYS = 1;
  161. const BCMS_SDK_INIT_FAIL = 2;
  162. const BCMS_SDK_PARAM = 3;
  163. const BCMS_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR = 4;
  164. const BCMS_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR = 5;
  165. /**
  166. * 错误常量与错误字符串的映射
  167. *
  168. * 用户关注:否
  169. */
  170. private $_arrayErrorMap = array
  171. (
  172. '0' => 'php sdk error',
  173. self::BCMS_SDK_SYS => 'php sdk error',
  174. self::BCMS_SDK_INIT_FAIL => 'php sdk init error',
  175. self::BCMS_SDK_PARAM => 'lack param',
  176. self::BCMS_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR => 'http status is error, and the body returned is not a json string',
  177. self::BCMS_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR => 'http status is ok, but the body returned is not a json string',
  178. );
  179. /**
  180. * setAccessKey
  181. *
  182. * 用户关注:是
  183. * 服务类方法, 设置Bcms对象的accessKey属性,如果用户在创建Bcms对象时已经通过参数设置了accessKey,这里的设置将会覆盖以前的设置
  184. *
  185. * @access public
  186. * @param string $accessKey
  187. * @return 成功:true,失败:false
  188. * @throws BcmsException
  189. *
  190. * @version 1.2.0
  191. */
  192. public function setAccessKey ( $accessKey )
  193. {
  194. $this->_resetErrorStatus ( );
  195. try
  196. {
  197. if( $this->_checkString ( $accessKey, 1, 64 ) )
  198. {
  199. $this->_clientId = $accessKey;
  200. }
  201. else
  202. {
  203. throw new BcmsException ( "invaid access_key ( ${accessKey} ), which must be a 1-64 length string", self::BCMS_SDK_INIT_FAIL );
  204. }
  205. }
  206. catch ( Exception $ex )
  207. {
  208. $this->_bcmsExceptionHandler ( $ex );
  209. return false;
  210. }
  211. return true;
  212. }
  213. /**
  214. * setSecretKey
  215. *
  216. * 用户关注:是
  217. * 服务类方法, 设置Bcms对象的secretKey属性,如果用户在创建Bcms对象时已经通过参数设置了secretKey,这里的设置将会覆盖以前的设置
  218. *
  219. * @access public
  220. * @param string $secretKey
  221. * @return 成功:true,失败:false
  222. * @throws BcmsException
  223. *
  224. * @version 1.2.0
  225. */
  226. public function setSecretKey ( $secretKey )
  227. {
  228. $this->_resetErrorStatus ( );
  229. try
  230. {
  231. if( $this->_checkString ( $secretKey, 1, 64 ) )
  232. {
  233. $this->_clientSecret = $secretKey;
  234. }
  235. else
  236. {
  237. throw new BcmsException ( "invaid secret_key ( ${secretKey} ), which must be a 1-64 length string", self::BCMS_SDK_INIT_FAIL );
  238. }
  239. }
  240. catch ( Exception $ex )
  241. {
  242. $this->_bcmsExceptionHandler ( $ex );
  243. return false;
  244. }
  245. return true;
  246. }
  247. /**
  248. * setAccessAndSecretKey
  249. *
  250. * 用户关注:是
  251. * 服务类方法, 同时设置Bcms对象的AcessKey、secretKey属性,如果用户在创建Bcms对象时已经通过参数设置了
  252. * AccessKey、secretKey,这里的设置将会覆盖以前的设置
  253. *
  254. * @access public
  255. * @param string $accessKey
  256. * @param string $secretKey
  257. * @return 成功:true,失败:false
  258. * @throws BcmsException
  259. *
  260. * @version 1.2.0
  261. */
  262. public function setAccessAndSecretKey ( $accessKey, $secretKey )
  263. {
  264. $this->_resetErrorStatus ( );
  265. try
  266. {
  267. if( ! $this->_checkString ( $accessKey, 1, 64 ) )
  268. {
  269. throw new BcmsException ( "invaid access_key ( ${accessKey} ), which must be a 1-64 length string", self::BCMS_SDK_INIT_FAIL );
  270. }
  271. if( ! $this->_checkString ( $secretKey, 1, 64 ) )
  272. {
  273. throw new BcmsException ( "invaid secret_key ( ${secretKey} ), which must be a 1-64 length string", self::BCMS_SDK_INIT_FAIL );
  274. }
  275. $this->_clientId = $accessKey;
  276. $this->_clientSecret = $secretKey;
  277. }
  278. catch ( Exception $ex )
  279. {
  280. $this->_bcmsExceptionHandler ( $ex );
  281. return false;
  282. }
  283. return true;
  284. }
  285. /**
  286. * setHost
  287. *
  288. * 用户关注:是
  289. * 服务类方法, 设置Bcms对象的host属性,如果用户在创建Bcms对象时已经通过参数设置了host,这里的设置将会覆盖以前的设置
  290. *
  291. * @access public
  292. * @param string $host
  293. * @return 成功:true,失败:false
  294. * @throws BcmsException
  295. *
  296. * @version 1.2.0
  297. */
  298. public function setHost ( $host )
  299. {
  300. $this->_resetErrorStatus ( );
  301. try
  302. {
  303. if ( $this->_checkString ( $host, 1, 1024 ) )
  304. {
  305. $this->_host = $host;
  306. }
  307. else
  308. {
  309. throw new BcmsException ( "invaid host ( ${host} ), which must be a 1 - 1024 length string", self::BCMS_SDK_INIT_FAIL );
  310. }
  311. }
  312. catch ( Exception $ex )
  313. {
  314. $this->_bcmsExceptionHandler ( $ex );
  315. return false;
  316. }
  317. return true;
  318. }
  319. /**
  320. * setCurlOpts
  321. *
  322. * 用户关注:是
  323. * 服务类方法, 设置HTTP交互的OPTION,同PHP curl库的所有opt参数
  324. *
  325. * @access public
  326. * @param array $arr_curlopt
  327. * @return 成功:true,失败:false
  328. * @throws BcmsException
  329. *
  330. * @version 1.2.0
  331. */
  332. public function setCurlOpts ( $arr_curlOpts )
  333. {
  334. $this->_resetErrorStatus ( );
  335. try
  336. {
  337. if ( is_array ( $arr_curlOpts ) )
  338. {
  339. foreach ( $arr_curlOpts as $k => $v )
  340. {
  341. $this->_curlOpts [ $k ] = $v;
  342. }
  343. }
  344. else
  345. {
  346. throw new BcmsException( 'invalid param - arr_curlOpts is not an array [' . print_r( $arr_curlOpts, true ) . ']', self::BCMS_SDK_INIT_FAIL);
  347. }
  348. }
  349. catch ( Exception $ex )
  350. {
  351. $this->_bcmsExceptionHandler ( $ex );
  352. return false;
  353. }
  354. return true;
  355. }
  356. /**
  357. * getRequestId
  358. *
  359. * 用户关注:是
  360. * 服务类方法,获取上次调用的request_id,如果SDK本身错误,则直接返回0
  361. *
  362. * @access public
  363. * @return 上次调用服务器返回的request_id
  364. *
  365. * @version 1.2.0
  366. */
  367. public function getRequestId ( )
  368. {
  369. return $this->_requestId;
  370. }
  371. /**
  372. * createQueue
  373. *
  374. * 用户关注:是
  375. * 服务类方法,创建一个队列
  376. *
  377. * @access public
  378. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION、Bcms::QUEUE_TYPE、Bcms::QUEUE_ALIAS_NAME
  379. * @return 成功:包含执行结果的php数组;失败:false
  380. *
  381. * @version 1.2.0
  382. */
  383. public function createQueue ( $optional = NULL )
  384. {
  385. $this->_resetErrorStatus ( );
  386. try
  387. {
  388. $tmpArgs = func_get_args ( );
  389. $arrArgs = $this->_mergeArgs ( array ( ), $tmpArgs );
  390. $arrArgs [ self::METHOD ] = 'create';
  391. $arrArgs [ self::QUEUE_NAME ] = 'queue';
  392. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME ) );
  393. }
  394. catch ( Exception $ex )
  395. {
  396. $this->_bcmsExceptionHandler ( $ex );
  397. return false;
  398. }
  399. }
  400. /**
  401. * dropQueue
  402. *
  403. * 用户关注:是
  404. * 服务类方法,删除一个队列
  405. *
  406. * @access public
  407. * @param string $queueName 要删除的队列名
  408. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  409. * @return 成功:包含执行结果的php数组;失败:false
  410. *
  411. * @version 1.2.0
  412. */
  413. public function dropQueue ( $queueName, $optional = NULL )
  414. {
  415. $this->_resetErrorStatus ( );
  416. try
  417. {
  418. $tmpArgs = func_get_args ( );
  419. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME ), $tmpArgs );
  420. $arrArgs [ self::METHOD ] = 'drop';
  421. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME ) );
  422. }
  423. catch ( Exception $ex )
  424. {
  425. $this->_bcmsExceptionHandler ( $ex );
  426. return false;
  427. }
  428. }
  429. /**
  430. * subscribeQueue
  431. *
  432. * 用户关注:是
  433. * 服务类方法,订阅一个队列,BCMS会向目标地址destination发送一个确认请求,确认请求中会带上 $queueName、$destination、
  434. * $token三个信息;被订阅者需要明确回复一个json包 '{"result":0}',BCMS才会认为订阅成功,否则,BCMS会将此订阅视为pending
  435. * 状态,返回失败;如果在订阅时,被订阅的目标地址尚未准备好,那么后续被订阅者可以调用confirmQueue,带上 $queueName、
  436. * $destination、$token信息来确认这个订阅,
  437. * 确认订阅成功后,BCMS将该订阅的状态从pending改为normal,以后有消息到达时,会向目标地址推送消息
  438. * @see confirmQueue
  439. *
  440. * @access public
  441. * @param string $queueName 要订阅的队列名
  442. * @param string $destination 订阅的目的地址
  443. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  444. * @return 成功:包含执行结果的php数组;失败:false
  445. *
  446. * @version 1.2.0
  447. */
  448. public function subscribeQueue ( $queueName, $destination, $optional = array ( ) )
  449. {
  450. $this->_resetErrorStatus ( );
  451. try
  452. {
  453. $tmpArgs = func_get_args ( );
  454. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::DESTINATION ), $tmpArgs );
  455. $arrArgs [ self::METHOD ] = 'subscribe';
  456. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::DESTINATION ) );
  457. }
  458. catch ( Exception $ex )
  459. {
  460. $this->_bcmsExceptionHandler ( $ex );
  461. return false;
  462. }
  463. }
  464. /**
  465. * unsubscribeQueue
  466. *
  467. * 用户关注:是
  468. * 服务类方法,取消某个队列的某个订阅
  469. *
  470. * @access public
  471. * @param string $queueName 取消订阅的队列名
  472. * @param string $destination 取消订阅的目标地址
  473. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  474. * @return 成功:包含执行结果的php数组;失败:false
  475. *
  476. * @version 1.2.0
  477. */
  478. public function unsubscribeQueue ( $queueName, $destination, $optional = array ( ) )
  479. {
  480. $this->_resetErrorStatus ( );
  481. try
  482. {
  483. $tmpArgs = func_get_args ( );
  484. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::DESTINATION ), $tmpArgs );
  485. $arrArgs [ self::METHOD ] = 'unsubscribe';
  486. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::DESTINATION ) );
  487. }
  488. catch ( Exception $ex )
  489. {
  490. $this->_bcmsExceptionHandler ( $ex );
  491. return false;
  492. }
  493. }
  494. /**
  495. * unsubscribeAllQueue
  496. *
  497. * 用户关注:是
  498. * 服务类方法,取消某个队列所有的订阅
  499. *
  500. * @access public
  501. * @param string $queueName 要取消哪个队列的所有订阅
  502. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  503. * @return 成功:包含执行结果的php数组;失败:false
  504. *
  505. * @version 1.2.0
  506. */
  507. public function unsubscribeAllQueue ( $queueName, $optional = array ( ) )
  508. {
  509. $this->_resetErrorStatus ( );
  510. try
  511. {
  512. $tmpArgs = func_get_args ( );
  513. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME ), $tmpArgs );
  514. $arrArgs [ self::METHOD ] = 'unsubscribeall';
  515. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME ) );
  516. }
  517. catch ( Exception $ex )
  518. {
  519. $this->_bcmsExceptionHandler ( $ex );
  520. return false;
  521. }
  522. }
  523. /**
  524. * grantQueue
  525. *
  526. * 用户关注:是
  527. * 服务类方法,将一个队列的某些权限授予(分享)给其他(她)用户
  528. *
  529. * @access public
  530. * @param string $queueName 要将那个队列的权限授予(分享)给其他(她)用户
  531. * @param string $label 授权时,为这次授权赋予一个标签,以后收回权限时,凭借此标签
  532. * @param string $user 给哪个用户授权
  533. * @param int $userType $user的类型,1: 百度passport用户名;2:百度passport邮箱名;3:百度passport手机号
  534. * @param string $actions 要授予哪些权限,以json的格式给出, 必须utf-8编码,否则服务器会报参数错误,可以一次授予多个权限
  535. * @see SDK wiki文档
  536. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION、Bcms::EFFECT_START、Bcms::EFFECT_END
  537. * @return 成功:包含执行结果的php数组;失败:false
  538. *
  539. * @version 1.2.0
  540. */
  541. public function grantQueue ( $queueName, $label, $user, $userType, $actions, $optional = array ( ) )
  542. {
  543. $this->_resetErrorStatus ( );
  544. try
  545. {
  546. $tmpArgs = func_get_args ( );
  547. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::LABEL, self::USER, self::USERTYPE, self::ACTIONS ), $tmpArgs );
  548. $arrArgs [ self::METHOD ] = 'grant';
  549. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::LABEL, self::USER, self::USERTYPE, self::ACTIONS ) );
  550. }
  551. catch ( Exception $ex )
  552. {
  553. $this->_bcmsExceptionHandler ( $ex );
  554. return false;
  555. }
  556. }
  557. /**
  558. * revokeQueue
  559. *
  560. * 用户关注:是
  561. * 服务类方法,回收权限
  562. *
  563. * @param string $queueName 要回收哪个队列的权限
  564. * @param string $label 在授权时,指定的label,这里表示要回收这个权限
  565. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  566. * @return 成功:包含执行结果的php数组;失败:false
  567. *
  568. * @version 1.2.0
  569. */
  570. public function revokeQueue ( $queueName, $label, $optional = array ( ) )
  571. {
  572. $this->_resetErrorStatus ( );
  573. try
  574. {
  575. $tmpArgs = func_get_args ( );
  576. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::LABEL ), $tmpArgs );
  577. $arrArgs [ self::METHOD ] = 'revoke';
  578. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::LABEL ) );
  579. }
  580. catch ( Exception $ex )
  581. {
  582. $this->_bcmsExceptionHandler ( $ex );
  583. return false;
  584. }
  585. }
  586. /**
  587. * suspendQueue
  588. *
  589. * 用户关注:是
  590. * 服务类方法,将一个队列暂停
  591. *
  592. * @access public
  593. * @param string $queueName 要暂停的队列名
  594. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  595. * @return 成功:包含执行结果的php数组;失败:false
  596. *
  597. * @version 1.2.0
  598. */
  599. public function suspendQueue ( $queueName, $optional = array ( ) )
  600. {
  601. $this->_resetErrorStatus ( );
  602. try
  603. {
  604. $tmpArgs = func_get_args ( );
  605. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME ), $tmpArgs );
  606. $arrArgs [ self::METHOD ] = 'suspend';
  607. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME ) );
  608. }
  609. catch ( Exception $ex )
  610. {
  611. $this->_bcmsExceptionHandler ( $ex );
  612. return false;
  613. }
  614. }
  615. /**
  616. * resumeQueue
  617. *
  618. * 用户关注:是
  619. * 服务类方法,将一个队列从暂停状态恢复成正常状态
  620. *
  621. * @access public
  622. * @param string $queueName 要恢复的队列名
  623. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  624. * @return 成功:包含执行结果的php数组;失败:false
  625. *
  626. * @version 1.2.0
  627. */
  628. public function resumeQueue ( $queueName, $optional = array ( ) )
  629. {
  630. $this->_resetErrorStatus ( );
  631. try
  632. {
  633. $tmpArgs = func_get_args ( );
  634. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME ), $tmpArgs );
  635. $arrArgs [ self::METHOD ] = 'resume';
  636. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME ) );
  637. }
  638. catch ( Exception $ex )
  639. {
  640. $this->_bcmsExceptionHandler ( $ex );
  641. return false;
  642. }
  643. }
  644. /**
  645. * confirmQueue
  646. *
  647. * 用户关注:是
  648. * 服务类方法,被订阅者调用该方法,确认订阅,告诉BCMS,目标地址同意订阅,可以往我这里推送消息
  649. *
  650. * @access public
  651. * @param string $queueName 要确认订阅的队列名
  652. * @param string $token 本次队列确认需要的token
  653. * @param string $destination 确认订阅的目标地址
  654. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  655. * @return 成功:包含执行结果的php数组;失败:false
  656. *
  657. * @version 1.2.0
  658. */
  659. public function confirmQueue ( $queueName, $token, $destination, $optional = array ( ) )
  660. {
  661. $this->_resetErrorStatus ( );
  662. try
  663. {
  664. $tmpArgs = func_get_args ( );
  665. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::TOKEN, self::DESTINATION ), $tmpArgs );
  666. $arrArgs [ self::METHOD ] = 'confirm';
  667. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::TOKEN, self::DESTINATION ) );
  668. }
  669. catch ( Exception $ex )
  670. {
  671. $this->_bcmsExceptionHandler ( $ex );
  672. return false;
  673. }
  674. }
  675. /**
  676. * cancelQueue
  677. *
  678. * 用户关注:是
  679. * 服务类方法,被订阅者调用该方法,取消订阅,告诉BCMS,不要再往我这个地址推送消息了
  680. *
  681. * @access public
  682. * @param string $queueName 要取消订阅的队列名
  683. * @param string $token 本次队列取消需要的token
  684. * @param string $destination 取消订阅的目标地址
  685. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  686. * @return 成功:包含执行结果的php数组;失败:false
  687. *
  688. * @version 1.2.0
  689. */
  690. public function cancelQueue ( $queueName, $token, $destination, $optional = array ( ) )
  691. {
  692. $this->_resetErrorStatus ( );
  693. try
  694. {
  695. $tmpArgs = func_get_args ( );
  696. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::TOKEN, self::DESTINATION ), $tmpArgs );
  697. $arrArgs [ self::METHOD ] = 'cancel';
  698. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::TOKEN, self::DESTINATION ) );
  699. }
  700. catch ( Exception $ex )
  701. {
  702. $this->_bcmsExceptionHandler ( $ex );
  703. return false;
  704. }
  705. }
  706. /**
  707. * publishMessage
  708. *
  709. * 用户关注:是
  710. * 服务类方法,向一个队列中发送一条消息
  711. *
  712. * @access public
  713. * @param string $queueName 要发送消息的队列名
  714. * @param binary $message 要发送的消息,支持任何类型的消息
  715. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  716. * @return 成功:包含执行结果的php数组;失败:false
  717. *
  718. * @version 1.2.0
  719. */
  720. public function publishMessage ( $queueName, $message, $optional = array ( ) )
  721. {
  722. $this->_resetErrorStatus ( );
  723. try
  724. {
  725. $tmpArgs = func_get_args ( );
  726. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::MESSAGE ), $tmpArgs );
  727. $arrArgs [ self::METHOD ] = 'publish';
  728. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::MESSAGE ) );
  729. }
  730. catch ( Exception $ex )
  731. {
  732. $this->_bcmsExceptionHandler ( $ex );
  733. return false;
  734. }
  735. }
  736. /**
  737. * publishMessages
  738. *
  739. * 用户关注:是
  740. * 服务类方法,向一个队列中批量发送消息
  741. *
  742. * @access public
  743. * @param string $queueName 要发送消息的队列名
  744. * @param binary $message 要发送的消息,以json格式给出,必须utf-8编码,否则服务器会报错,参数错误
  745. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  746. * @return 成功:包含执行结果的php数组;失败:false
  747. *
  748. * @version 1.2.0
  749. */
  750. public function publishMessages ( $queueName, $message, $optional = array ( ) )
  751. {
  752. $this->_resetErrorStatus ( );
  753. try
  754. {
  755. $tmpArgs = func_get_args ( );
  756. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::MESSAGE ), $tmpArgs );
  757. $arrArgs [ self::METHOD ] = 'publishes';
  758. $arrArgs [ self::MESSAGE ] = $this->_arrayToString ( $arrArgs [ self::MESSAGE ] );
  759. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::MESSAGE ) );
  760. }
  761. catch ( Exception $ex )
  762. {
  763. $this->_bcmsExceptionHandler ( $ex );
  764. return false;
  765. }
  766. }
  767. /**
  768. * fetchMessage
  769. *
  770. * 用户关注:是
  771. * 服务类方法,从一个队列中批量获取消息
  772. *
  773. * @access public
  774. * @param string $queueName 要获取哪个队列的消息
  775. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION,Bcms::MSG_ID、Bcms::FETCH_NUM
  776. * 需要说明的是:对于多模式队列,必须指定Bcms::MSG_ID,对于单模式队列,不能指定MSG_ID
  777. * @return 成功:包含执行结果的php数组;失败:false
  778. *
  779. * @version 1.2.0
  780. */
  781. public function fetchMessage ( $queueName, $optional = array ( ) )
  782. {
  783. $this->_resetErrorStatus ( );
  784. try
  785. {
  786. $tmpArgs = func_get_args ( );
  787. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME ), $tmpArgs );
  788. $arrArgs [ self::METHOD ] = 'fetch';
  789. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME ) );
  790. }
  791. catch ( Exception $ex )
  792. {
  793. $this->_bcmsExceptionHandler ( $ex );
  794. return false;
  795. }
  796. }
  797. /**
  798. * mail
  799. *
  800. * 用户关注:是
  801. * 服务类方法,向一个队列中发送邮件
  802. *
  803. * @access public
  804. * @param string $queueName 要发送邮件的队列名
  805. * @param string $message 要发送的邮件正文
  806. * @param string $address 要发往哪些地址,以json格式给出,必须utf-8编码,否则服务器会报参数错误
  807. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION、Bcms::FROM、Bcms::MAIL_SUBJECT
  808. * @return 成功:包含执行结果的php数组;失败:false
  809. *
  810. * @version 1.2.0
  811. */
  812. public function mail ( $queueName, $message, $address, $optional = array ( ) )
  813. {
  814. $this->_resetErrorStatus ( );
  815. try
  816. {
  817. $tmpArgs = func_get_args ( );
  818. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::MESSAGE, self::ADDRESS ), $tmpArgs );
  819. $arrArgs [ self::METHOD ] = 'mail';
  820. $arrArgs [ self::ADDRESS ] = $this->_arrayToString ( $arrArgs [ self::ADDRESS ] );
  821. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::MESSAGE, self::ADDRESS ) );
  822. }
  823. catch ( Exception $ex )
  824. {
  825. $this->_bcmsExceptionHandler ( $ex );
  826. return false;
  827. }
  828. }
  829. /**
  830. * sms
  831. *
  832. * 用户关注:是
  833. * 服务类方法,向一个队列中发送短信
  834. *
  835. * @access public
  836. * @param string $queueName 要发送邮件的队列名字
  837. * @param string $message 要发送的短信内容
  838. * @param string $address 要发往哪些手机号,以json格式给出,必须utf-8编码,否则服务器会报参数错误
  839. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  840. * @return 成功:包含执行结果的php数组;失败:false
  841. *
  842. * @version 1.2.0
  843. */
  844. public function sms ( $queueName, $message, $address, $optional = array ( ) )
  845. {
  846. $this->_resetErrorStatus ( );
  847. try
  848. {
  849. $tmpArgs = func_get_args ( );
  850. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::MESSAGE, self::ADDRESS ), $tmpArgs );
  851. $arrArgs [ self::METHOD ] = 'sms';
  852. $arrArgs [ self::ADDRESS ] = $this->_arrayToString ( $arrArgs [ self::ADDRESS ] );
  853. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::MESSAGE, self::ADDRESS ) );
  854. }
  855. catch ( Exception $ex )
  856. {
  857. $this->_bcmsExceptionHandler ( $ex );
  858. return false;
  859. }
  860. }
  861. /**
  862. * deleteMessageById
  863. *
  864. * 用户关注:是
  865. * 服务类方法,删除一个队列中的一条消息
  866. *
  867. * @access public
  868. * @param string $queueName 要删除消息的队列名
  869. * @param int $msgId 要删除的消息ID号
  870. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  871. * @return 成功:包含执行结果的php数组;失败:false
  872. *
  873. * @version 1.2.0
  874. */
  875. public function deleteMessageById ( $queueName, $msgId, $optional = array ( ) )
  876. {
  877. $this->_resetErrorStatus ( );
  878. try
  879. {
  880. $tmpArgs = func_get_args ( );
  881. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::MSG_ID ), $tmpArgs );
  882. $arrArgs [ self::METHOD ] = 'deletemessagebyid';
  883. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::MSG_ID ) );
  884. }
  885. catch ( Exception $ex )
  886. {
  887. $this->_bcmsExceptionHandler ( $ex );
  888. return false;
  889. }
  890. }
  891. /**
  892. * deleteMessagesByIds
  893. *
  894. * 用户关注:是
  895. * 服务类方法,批量从一个队列中删除消息,最多支持一次删除10个消息
  896. *
  897. * @access public
  898. * @param string $queueName 要删除消息的队列名
  899. * @param string $msgId 要删除消息的ID号,以json格式给出,必须utf-8编码,否则服务器会报参数错误
  900. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  901. * @return 成功:包含执行结果的php数组;失败:false
  902. *
  903. * @version 1.2.0
  904. */
  905. public function deleteMessagesByIds ( $queueName, $msgId, $optional = array ( ) )
  906. {
  907. $this->_resetErrorStatus ( );
  908. try
  909. {
  910. $tmpArgs = func_get_args ( );
  911. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::MSG_ID ), $tmpArgs );
  912. $arrArgs [ self::METHOD ] = 'deletemessagesbyids';
  913. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::MSG_ID ) );
  914. }
  915. catch ( Exception $ex )
  916. {
  917. $this->_bcmsExceptionHandler ( $ex );
  918. return false;
  919. }
  920. }
  921. /**
  922. * confirmMessage
  923. *
  924. * 用户关注:否
  925. * 服务类方法,确认一个消息已经被使用(内部方法,外部用户无需关注)
  926. *
  927. * @access public
  928. * @param string $queueName 要确认哪个消息的队列
  929. * @param string $msgId 要确认消息的ID号
  930. * @param string $msgTimeout 要确认消息的超时时间戳
  931. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  932. * @return 成功:包含执行结果的php数组;失败:false
  933. *
  934. * @version 1.2.0
  935. */
  936. public function confirmMessage ( $queueName, $msgId, $msgTimeout, $optional = array ( ) )
  937. {
  938. $this->_resetErrorStatus ( );
  939. try
  940. {
  941. $tmpArgs = func_get_args ( );
  942. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME, self::MSG_ID, self::MSG_TIMEOUT ), $tmpArgs );
  943. $arrArgs [ self::METHOD ] = 'confirmmsg';
  944. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME, self::MSG_ID, self::MSG_TIMEOUT ) );
  945. }
  946. catch ( Exception $ex )
  947. {
  948. $this->_bcmsExceptionHandler ( $ex );
  949. return false;
  950. }
  951. }
  952. /**
  953. * getMaxMsgId
  954. *
  955. * 用户关注:是
  956. * 服务类方法,获取一个队列中最大的消息ID号
  957. *
  958. * @access public
  959. * @param string $queueName 要获取哪个队列的最大消息ID号
  960. * @param array $optional 可选参数数组,可以设置的非必须参数的key包括:Bcms::VERSION
  961. * @return 成功:包含执行结果的php数组;失败:false
  962. *
  963. * @version 1.2.0
  964. */
  965. public function getMaxMsgId ( $queueName, $optional = array ( ) )
  966. {
  967. $this->_resetErrorStatus ( );
  968. try
  969. {
  970. $tmpArgs = func_get_args ( );
  971. $arrArgs = $this->_mergeArgs ( array ( self::QUEUE_NAME ), $tmpArgs );
  972. $arrArgs [ self::METHOD ] = 'getmaxmsgid';
  973. return $this->_commonProcess ( $arrArgs, array ( self::QUEUE_NAME ) );
  974. }
  975. catch ( Exception $ex )
  976. {
  977. $this->_bcmsExceptionHandler ( $ex );
  978. return false;
  979. }
  980. }
  981. /**
  982. * __construct
  983. *
  984. * 用户关注:是
  985. *
  986. * 对象构造方法,用户可以传入$accessKey、$secretKey、$host进行初始化
  987. * 如果用户没有传入$accessKey、$secretKey、$host,这三个参数可以其他几个地方予以设置,如下:
  988. * 1. 在调用SDK时,在$optional参数中设置,如$optional[Cron::SECRET_KEY] = 'my_secret_key',影响范围:本次SDK调用
  989. * 2. 调用SDK对象的setXXX系列函数进行设置,如$bcms->setSecretKey('my_secret_key'),影响范围:自设置之后起的每次SDK调用
  990. * 3. 全局变量,如g_secretKey = 'my_secret_key',影响范围:当1、2均无法获取到$accessKey、$secretKey、$host时,会从全局变量中获取
  991. * 说明:SDK获取$accessKey、$secretKey、$host的优先级是:
  992. * 1. SDK的$optional参数
  993. * 2. Bcms对象的属性(通过初始化参数或setXXX系列函数指定)
  994. * 3. 全局变量
  995. *
  996. * @access public
  997. * @param string $accessKey 用户在管理平台上申请到的accessKey
  998. * @param string $secretKey 用户在管理平台上申请到的SecretKey
  999. * @param string $host 百度定时服务API的域名,不包括http://
  1000. * @throws BcmsException 如果出错,则抛出异常,异常号是self::BCMS_SDK_INIT_FAIL
  1001. *
  1002. * @version 1.2.0
  1003. */
  1004. public function __construct ( $accessKey = NULL, $secretKey = NULL, $host = NULL, $arr_curlOpts = array() )
  1005. {
  1006. if( is_null ( $accessKey ) || $this->_checkString ( $accessKey, 1, 64 ) )
  1007. {
  1008. $this->_clientId = $accessKey;
  1009. }
  1010. else
  1011. {
  1012. throw new BcmsException ( "invalid param - access key [ ${accessKey} ] , which must be a 1 - 64 length string", self::BCMS_SDK_INIT_FAIL );
  1013. }
  1014. if( is_null ( $secretKey ) || $this->_checkString ( $secretKey, 1, 64 ) )
  1015. {
  1016. $this->_clientSecret = $secretKey;
  1017. }
  1018. else
  1019. {
  1020. throw new BcmsException ( "invalid param - secret key [ ${secretKey} ] , which must be a 1 - 64 length string", self::BCMS_SDK_INIT_FAIL );
  1021. }
  1022. if( is_null ( $host ) || $this->_checkString ( $host, 1, 1024 ) )
  1023. {
  1024. if ( !is_null ( $host ) )
  1025. {
  1026. $this->_host = $host;
  1027. }
  1028. }
  1029. else
  1030. {
  1031. throw new BcmsException ( "invalid param - host [ ${host} ] , which must be a 1 - 1024 length string", self::BCMS_SDK_INIT_FAIL );
  1032. }
  1033. if ( !is_array($arr_curlOpts) )
  1034. {
  1035. throw new BcmsException( 'invalid param - arr_curlopt is not an array [' . print_r( $arr_curlOpts, true ) . ']', self::BCMS_SDK_INIT_FAIL);
  1036. }
  1037. foreach ( $arr_curlOpts as $k => $v )
  1038. {
  1039. $this->_curlOpts [ $k ] = $v;
  1040. }
  1041. $this->_resetErrorStatus();
  1042. }
  1043. /**
  1044. * _checkString
  1045. *
  1046. * 用户关注:否
  1047. *
  1048. * 检查参数是否是一个大于等于$min且小于等于$max的字符串
  1049. *
  1050. * @access private
  1051. * @param string $str 要检查的字符串
  1052. * @param int $min 字符串最小长度
  1053. * @param int $max 字符串最大长度
  1054. * @return 成功:true;失败:false
  1055. *
  1056. * @version 1.2.0
  1057. */
  1058. private function _checkString ( $str, $min, $max )
  1059. {
  1060. if ( is_string ( $str ) && strlen ( $str ) >= $min && strlen ( $str ) <= $max )
  1061. {
  1062. return true;
  1063. }
  1064. return false;
  1065. }
  1066. /**
  1067. * _get_ak_sk_host
  1068. *
  1069. * 用户关注:否
  1070. * 获取AK/SK/HOST的统一过程函数
  1071. *
  1072. * @access private
  1073. * @param array $opt 参数数组
  1074. * @param string $opt_key 参数数组的key
  1075. * @param string $member 对象成员
  1076. * @param string $g_key 全局变量的名字
  1077. * @param string $env_key 环境变量的名字
  1078. * @param int $min 字符串最短值
  1079. * @param int $max 字符串最长值
  1080. * @throws BcmsException 如果出错,则抛出BcmsException异常,异常类型为self::BCMS_SDK_PARAM
  1081. *
  1082. * @version 1.2.0
  1083. */
  1084. private function _get_ak_sk_host ( &$opt, $opt_key, $member, $g_key, $env_key, $min, $max )
  1085. {
  1086. $dis = array ( 'client_id' => 'access_key' , 'client_secret' => 'secret_key', 'host' => 'host' );
  1087. global $$g_key;
  1088. if ( isset ( $opt [ $opt_key ] ) )
  1089. {
  1090. if ( ! $this->_checkString ( $opt [ $opt_key ], $min, $max ) )
  1091. {
  1092. throw new BcmsException ( 'invalid ' . $dis [ $opt_key ] . ' in $optinal ( ' . $opt [ $opt_key ] . ' ) , which must be a ' . $min . ' - ' . $max . ' length string', self::BCMS_SDK_PARAM );
  1093. }
  1094. return ;
  1095. }
  1096. if ( $this->_checkString ( $member, $min, $max ) )
  1097. {
  1098. $opt [ $opt_key ] = $member;
  1099. return ;
  1100. }
  1101. if ( isset ( $$g_key ) )
  1102. {
  1103. if ( ! $this->_checkString ( $$g_key, $min, $max ) )
  1104. {
  1105. throw new BcmsException ( 'invalid ' . $g_key . ' in global area ( ' . $$g_key . ' ) , which must be a ' . $min . ' - ' . $max . ' length string', self::BCMS_SDK_PARAM );
  1106. }
  1107. $opt [ $opt_key ] = $$g_key;
  1108. return ;
  1109. }
  1110. if ( false !== getenv ( $env_key ) )
  1111. {
  1112. if ( ! $this->_checkString ( getenv ( $env_key ) , $min, $max ) )
  1113. {
  1114. throw new BcmsException ( 'invalid ' . $env_key . ' in environment variable ( ' . getenv ( $env_key ) . ' ) , which must be a ' . $min . ' - ' . $max . ' length string', self::BCMS_SDK_PARAM );
  1115. }
  1116. $opt [ $opt_key ] = getenv ( $env_key ) ;
  1117. return ;
  1118. }
  1119. if ( $opt_key === self::HOST )
  1120. {
  1121. $opt [ $opt_key ] = self::DEFAULT_HOST;
  1122. return ;
  1123. }
  1124. throw new BcmsException ( 'no param ( ' . $dis [ $opt_key ] . ' ) was found', self::BCMS_SDK_PARAM );
  1125. }
  1126. /**
  1127. * _adjustOpt
  1128. *
  1129. * 用户关注:否
  1130. *
  1131. * 参数调整方法
  1132. *
  1133. * @access private
  1134. * @param array $opt 参数数组
  1135. * @throws BcmsException 如果出错,则抛出异常,异常号为 self::BCMS_SDK_PARAM
  1136. *
  1137. * @version 1.2.0
  1138. */
  1139. private function _adjustOpt ( &$opt ) {
  1140. if ( ! isset ( $opt ) || empty ( $opt ) || ! is_array ( $opt ) )
  1141. {
  1142. throw new BcmsException ( 'no params are set', self::BCMS_SDK_PARAM );
  1143. }
  1144. if ( ! isset ( $opt [ self::TIMESTAMP ] ) )
  1145. {
  1146. $opt [ self::TIMESTAMP ] = time ( );
  1147. }
  1148. $this->_get_ak_sk_host ( $opt, self::ACCESS_KEY, $this->_clientId, 'g_accessKey', 'HTTP_BAE_ENV_AK', 1, 64 );
  1149. $this->_get_ak_sk_host ( $opt, self::SECRET_KEY, $this->_clientSecret, 'g_secretKey', 'HTTP_BAE_ENV_SK', 1, 64 );
  1150. $this->_get_ak_sk_host ( $opt, self::HOST, $this->_host, 'g_host', 'HTTP_BAE_ENV_ADDR_BCMS', 1, 1024 );
  1151. }
  1152. /**
  1153. * _bcmsGetSign
  1154. *
  1155. * 用户关注:否
  1156. *
  1157. * 签名方法
  1158. *
  1159. * @access private
  1160. * @param array $opt 参数数组
  1161. * @param array $arrContent 可以加入签名的参数数组,返回值
  1162. * @param array $arrNeed 必须的参数
  1163. * @throws BcmsException 如果出错,则抛出异常,异常号为self::BCMS_SDK_PARAM
  1164. *
  1165. * @version 1.2.0
  1166. */
  1167. private function _bcmsGetSign ( &$opt, &$arrContent, $arrNeed = array ( ) ) {
  1168. $arrData = array ( );
  1169. $arrContent = array ( );
  1170. $arrNeed [ ] = self::TIMESTAMP;
  1171. $arrNeed [ ] = self::METHOD;
  1172. $arrNeed [ ] = self::ACCESS_KEY;
  1173. if ( isset ( $opt [ self::EXPIRES ] ) )
  1174. {
  1175. $arrNeed [ ] = self::EXPIRES;
  1176. }
  1177. if ( isset ( $opt [ self::VERSION ] ) )
  1178. {
  1179. $arrNeed [ ] = self::VERSION;
  1180. }
  1181. $arrExclude = array ( self::QUEUE_NAME, self::HOST, self::SECRET_KEY );
  1182. foreach ( $arrNeed as $key )
  1183. {
  1184. if ( ! isset ( $opt [ $key ] ) || ( ! is_integer( $opt [ $key ] ) && empty ( $opt [ $key ] ) ) )
  1185. {
  1186. throw new BcmsException ( "lack param (${key})", self::BCMS_SDK_PARAM );
  1187. }
  1188. if ( in_array ( $key, $arrExclude ) )
  1189. {
  1190. continue;
  1191. }
  1192. $arrData [ $key ] = $opt [ $key ] ;
  1193. $arrContent [ $key ] = $opt [ $key ] ;
  1194. }
  1195. foreach ( $opt as $key => $value )
  1196. {
  1197. if ( ! in_array ( $key, $arrNeed ) && ! in_array ( $key, $arrExclude ) )
  1198. {
  1199. $arrData [ $key ] = $value;
  1200. $arrContent [ $key ] = $value;
  1201. }
  1202. }
  1203. ksort ( $arrData );
  1204. $url = 'http://' . $opt [ self::HOST ] . '/rest/2.0/' . self::PRODUCT . '/';
  1205. if ( isset($opt [ self::QUEUE_NAME ] ) && !is_null ( $opt [ self::QUEUE_NAME ] ) )
  1206. {
  1207. $url .= $opt [ self::QUEUE_NAME ] ;
  1208. $arrContent [ self::QUEUE_NAME ] = $opt [ self::QUEUE_NAME ] ;
  1209. }
  1210. else if ( isset($opt [ self::UID ] ) && !is_null ( $opt [ self::UID ] ) )
  1211. {
  1212. $url .= $opt [ self::UID ] ;
  1213. }
  1214. else
  1215. {
  1216. $url .= 'queue';
  1217. }
  1218. $basicString = 'POST' . $url;
  1219. foreach ( $arrData as $key => $value )
  1220. {
  1221. $basicString .= $key . '=' . $value;
  1222. }
  1223. $basicString .= $opt [ self::SECRET_KEY ] ;
  1224. $sign = md5 ( urlencode ( $basicString ) );
  1225. $arrContent [ self::SIGN ] = $sign;
  1226. $arrContent [ self::HOST ] = $opt [ self::HOST ];
  1227. }
  1228. /**
  1229. * _baseControl
  1230. *
  1231. * 用户关注:否
  1232. *
  1233. * 网络交互方法
  1234. *
  1235. * @access private
  1236. * @param array $opt 参数数组
  1237. * @throws BcmsException 如果出错,则抛出异常,错误号为self::BCMS_SDK_SYS
  1238. *
  1239. * @version 1.2.0
  1240. */
  1241. private function _baseControl ( $opt )
  1242. {
  1243. $content = '';
  1244. $resource = 'queue';
  1245. if ( isset($opt [ self::QUEUE_NAME ] ) && !is_null ( $opt [ self::QUEUE_NAME ] ) )
  1246. {
  1247. $resource = $opt [ self::QUEUE_NAME ] ;
  1248. unset( $opt [ self::QUEUE_NAME ] );
  1249. }
  1250. else if ( isset($opt [ self::UID ] ) && !is_null ( $opt [ self::UID ] ) )
  1251. {
  1252. $resource = $opt [ self::UID ] ;
  1253. }
  1254. $host = $opt [ self::HOST ];
  1255. unset ( $opt [ self::HOST ] );
  1256. foreach ( $opt as $k => $v )
  1257. {
  1258. if ( is_string ( $v ) )
  1259. {
  1260. $v = urlencode ( $v );
  1261. }
  1262. $content .= $k . '=' . $v . '&';
  1263. }
  1264. $content = substr ( $content, 0, strlen ( $content ) - 1 );
  1265. $url = 'http://' . $host . '/rest/2.0/' . self::PRODUCT . '/';
  1266. $url .= $resource;
  1267. $request = new RequestCore ( $url );
  1268. $headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded';
  1269. $headers [ 'User-Agent' ] = 'Baidu Message Service Phpsdk Client';
  1270. foreach ( $headers as $headerKey => $headerValue )
  1271. {
  1272. $headerValue = str_replace ( array ( "\r", "\n" ), '', $headerValue );
  1273. if ( $headerValue !== '' )
  1274. {
  1275. $request->add_header ( $headerKey, $headerValue );
  1276. }
  1277. }
  1278. $request->set_method ( 'POST' );
  1279. $request->set_body ( $content );
  1280. if ( is_array( $this->_curlOpts ) )
  1281. {
  1282. $request->set_curlOpts($this->_curlOpts);
  1283. }
  1284. $request->send_request ( );
  1285. return new ResponseCore ( $request->get_response_header ( ), $request->get_response_body ( ), $request->get_response_code ( ) );
  1286. }
  1287. /**
  1288. * _bcmsExceptionHandler
  1289. *
  1290. * 用户关注:否
  1291. *
  1292. * 异常处理方法
  1293. *
  1294. * @access private
  1295. * @param Excetpion $ex 异常处理函数,主要是填充Bcms对象的错误状态信息
  1296. *
  1297. * @version 1.2.0
  1298. */
  1299. private function _bcmsExceptionHandler ( $ex )
  1300. {
  1301. $tmpCode = $ex->getCode( );
  1302. if( 0 === $tmpCode )
  1303. {
  1304. $tmpCode = self::BCMS_SDK_SYS;
  1305. }
  1306. $this->errcode = $tmpCode;
  1307. if( $this->errcode >= 30000 )
  1308. {
  1309. $this->errmsg = $ex->getMessage ( );
  1310. }
  1311. else
  1312. {
  1313. $this->errmsg = $this->_arrayErrorMap [ $this->errcode ] . ', detail info [ ' . $ex->getMessage ( ) . ', break point: ' . $ex->getFile ( ) . ': ' . $ex->getLine ( ) . ' ] .';
  1314. }
  1315. }
  1316. /**
  1317. * _commonProcess
  1318. *
  1319. * 用户关注:否
  1320. *
  1321. * 所有服务类SDK方法的通用过程
  1322. *
  1323. * @access private
  1324. * @param array $paramOpt 参数数组
  1325. * @param array $arrNeed 必须的参数KEY
  1326. * @throws BcmsException 如果出错,则抛出异常
  1327. *
  1328. * @version 1.2.0
  1329. */
  1330. private function _commonProcess ( $paramOpt = NULL, $arrNeed = array ( ) )
  1331. {
  1332. $this->_adjustOpt ( $paramOpt );
  1333. $arrContent = array ( );
  1334. $this->_bcmsGetSign ( $paramOpt, $arrContent, $arrNeed );
  1335. $ret = $this->_baseControl ( $arrContent );
  1336. if ( empty ( $ret ) )
  1337. {
  1338. throw new BcmsException ( 'base control returned empty object', self::BCMS_SDK_SYS );
  1339. }
  1340. if ( $ret->isOK ( ) )
  1341. {
  1342. $result = json_decode ( $ret->body, true );
  1343. if ( is_null ( $result ) )
  1344. {
  1345. throw new BcmsException ( $ret->body, self::BCMS_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR );
  1346. }
  1347. $this->_requestId = $result [ 'request_id' ];
  1348. return $result;
  1349. }
  1350. $result = json_decode ( $ret->body, true );
  1351. if ( is_null ( $result ) )
  1352. {
  1353. throw new BcmsException ( 'ret body: ' . $ret->body, self::BCMS_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR );
  1354. }
  1355. $this->_requestId = $result [ 'request_id' ];
  1356. throw new BcmsException ( $result [ 'error_msg' ], $result [ 'error_code' ] );
  1357. }
  1358. /**
  1359. * _mergeArgs
  1360. *
  1361. * 用户关注:否
  1362. *
  1363. * 合并传入的参数到一个数组中,便于后续处理
  1364. *
  1365. * @access private
  1366. * @param array $arrNeed 必须的参数KEY
  1367. * @param array $tmpArgs 参数数组
  1368. * @throws BcmsException 如果出错,则抛出异常,异常号为self::Bcms_SDK_PARAM
  1369. *
  1370. * @version 1.2.0
  1371. */
  1372. private function _mergeArgs ( $arrNeed, $tmpArgs )
  1373. {
  1374. $arrArgs = array ( );
  1375. if ( 0 == count ( $arrNeed ) && 0 == count ( $tmpArgs ) )
  1376. {
  1377. return $arrArgs;
  1378. }
  1379. if ( count ( $tmpArgs ) - 1 != count ( $arrNeed ) && count ( $tmpArgs ) != count ( $arrNeed ) )
  1380. {
  1381. $keys = ' ( ';
  1382. foreach ( $arrNeed as $key )
  1383. {
  1384. $keys .= $key .= ', ';
  1385. }
  1386. if( $keys [ strlen($keys) - 1 ] === ' ' && ',' === $keys [ strlen($keys) - 2 ] )
  1387. {
  1388. $keys = substr($keys, 0, strlen($keys) - 2);
  1389. }
  1390. $keys .= ' ) ';
  1391. throw new Exception ( 'invalid sdk params, params' . $keys . 'are needed', self::BCMS_SDK_PARAM );
  1392. }
  1393. if ( count ( $tmpArgs ) - 1 == count ( $arrNeed ) && ! is_array ( $tmpArgs [ count ( $tmpArgs ) - 1 ] ) )
  1394. {
  1395. throw new Exception ( 'invalid sdk params, optional param must be an array', self::BCMS_SDK_PARAM );
  1396. }
  1397. $idx = 0;
  1398. foreach ( $arrNeed as $key )
  1399. {
  1400. if ( ! is_integer( $tmpArgs [ $idx ] ) && empty ( $tmpArgs [ $idx ] ) )
  1401. {
  1402. throw new Exception ( "lack param (${key})", self::BCMS_SDK_PARAM );
  1403. }
  1404. $arrArgs [ $key ] = $tmpArgs [ $idx ] ;
  1405. $idx += 1;
  1406. }
  1407. if ( isset($tmpArgs [ $idx ] ) )
  1408. {
  1409. foreach ( $tmpArgs [ $idx ] as $key => $value )
  1410. {
  1411. if ( ! array_key_exists ( $key, $arrArgs ) && ( is_integer($value) || ! empty ( $value ) ) )
  1412. {
  1413. $arrArgs [ $key ] = $value;
  1414. }
  1415. }
  1416. }
  1417. if ( isset ( $arrArgs [ self::QUEUE_NAME ] ) )
  1418. {
  1419. $arrArgs [ self::QUEUE_NAME ] = urlencode ( $arrArgs [ self::QUEUE_NAME ] );
  1420. }
  1421. return $arrArgs;
  1422. }
  1423. /**
  1424. * _resetErrorStatus
  1425. *
  1426. * 用户关注:否
  1427. *
  1428. * 恢复对象的错误状态,每次调用服务类方法时,由服务类方法自动调用该方法
  1429. *
  1430. * @access private
  1431. *
  1432. * @version 1.2.0
  1433. */
  1434. private function _resetErrorStatus ( )
  1435. {
  1436. $this->errcode = 0;
  1437. $this->errmsg = $this->_arrayErrorMap [ $this->errcode ] ;
  1438. $this->_requestId = 0;
  1439. }
  1440. /**
  1441. * _arrayToString
  1442. *
  1443. * 用户关注: 否
  1444. *
  1445. * 将array类型的对象转换成字符串
  1446. *
  1447. * @access private
  1448. * @param $arr 要转换array
  1449. * @throws BcmsException 如果出错,则抛出异常,异常号为self::Bcms_SDK_PARAM
  1450. *
  1451. * @version 1.2.0
  1452. */
  1453. function _arrayToString ( $arr )
  1454. {
  1455. if ( ! is_array ( $arr ) )
  1456. {
  1457. return $arr;
  1458. }
  1459. if( 0 === count ( $arr ) )
  1460. {
  1461. return '[]';
  1462. }
  1463. $ret = '[';
  1464. foreach ( $arr as $v )
  1465. {
  1466. $ret .= '"' . $v . '", ';
  1467. }
  1468. $ret = substr ( $ret, 0, strlen($ret) - 2 );
  1469. $ret .= ']';
  1470. return $ret;
  1471. }
  1472. };
  1473. ?>