You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

81 lines
2.0 KiB

  1. <?php
  2. /**
  3. * 远程接口连接器 Task_Runner_Remote_Connector
  4. *
  5. * @author dogstar <chanzonghuang@gmail.com> 20150516
  6. */
  7. abstract class Task_Runner_Remote_Connector {
  8. protected $host;
  9. protected $params = array();
  10. protected $moreParams = array();
  11. protected $url;
  12. protected $ret;
  13. protected $msg;
  14. protected $data = array();
  15. public function __construct($config) {
  16. $this->host = $config['host'];
  17. $this->moreParams = isset($config['params']) ? $config['params'] : array();
  18. }
  19. /**
  20. * 接口请求,超时时ret为404
  21. * @param string $service MQ中的接口服务名称,如:Default.Index
  22. * @param array $params 参数
  23. * @param int $timeoutMS 接口超时(单位:毫秒)
  24. * @return array
  25. */
  26. public function request($service, $params = array(), $timeoutMs = 3000) {
  27. $this->url = $this->host . '?service=' . $service;
  28. $params = array_merge($this->moreParams, $params);
  29. $apiRs = $this->doRequest($this->url, $params, $timeoutMs);
  30. if ($apiRs === FALSE) {
  31. $this->ret = 404;
  32. $this->msg = T('time out');
  33. DI()->logger->debug('task request api time out', array('url' => $this->url));
  34. return $this->getData();
  35. }
  36. $rs = json_decode($apiRs, true);
  37. if (empty($rs) || !isset($rs['ret'])) {
  38. $this->ret = 500;
  39. $this->msg = T('nothing return or illegal json: {rs}', array('rs' => $apiRs));
  40. return $this->getData();
  41. }
  42. $this->ret = $rs['ret'];
  43. $this->data = $rs['data'];
  44. $this->msg = $rs['msg'];
  45. return $this->getData();
  46. }
  47. public function getRet() {
  48. return $this->ret;
  49. }
  50. public function getData() {
  51. return $this->data;
  52. }
  53. public function getMsg() {
  54. return $this->msg;
  55. }
  56. public function getUrl() {
  57. return $this->url;
  58. }
  59. abstract protected function doRequest($url, $data, $timeoutMs);
  60. }