Platform.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. use Api\Lib\Platform\Field;
  4. use Api\Lib\Platform\Ssl;
  5. use Api\Lib\Platform\Request;
  6. use Api\Lib\Platform\Response;
  7. class Platform
  8. {
  9. protected $platform;
  10. protected $info;
  11. protected $field;
  12. protected function setting($id, $field = array())
  13. {
  14. $this->info($id);
  15. $this->platform();
  16. $this->field($field);
  17. return $this;
  18. }
  19. protected function info($id)
  20. {
  21. $this->info = is_array($id) ? $id : Dever::db($this->type, 'api')->find($id);
  22. if (!$this->info) {
  23. Dever::error('info error');
  24. }
  25. }
  26. protected function platform()
  27. {
  28. $this->platform = Dever::db('platform', 'api')->find($this->info['platform_id']);
  29. if (!$this->platform) {
  30. Dever::error('platform error');
  31. }
  32. if ($this->info['method'] == -1) {
  33. $this->info['method'] = $this->platform['method'];
  34. $this->info['post_method'] = $this->platform['post_method'];
  35. }
  36. if ($this->info['method'] == 1) {
  37. $this->info['post_method'] = 1;
  38. }
  39. }
  40. protected function field($field)
  41. {
  42. if (isset($field['account_project']) && isset($field['account_id'])) {
  43. $this->info['account_id'] = $field['account_id'];
  44. $this->info['account_project'] = $field['account_project'];
  45. $setting = Dever::db('account_setting', $field['account_project'])->select(array('account_id' => $field['account_id']));
  46. if ($setting) {
  47. foreach ($setting as $k => $v) {
  48. $info = Dever::db('platform_setting', 'api')->find($v['platform_setting_id']);
  49. if ($info) {
  50. $v['key'] = $info['key'];
  51. if (isset($field[$v['key']])) {
  52. $v['value'] = $field[$v['key']];
  53. }
  54. $field[$v['key']] = $v['value'];
  55. }
  56. }
  57. }
  58. } else {
  59. Dever::error('account error');
  60. }
  61. $this->field = new Field($field);
  62. $this->field->setPlatformId($this->platform['id']);
  63. $this->field->setApid($this->info['id']);
  64. $this->field->setHost($this->platform['host']);
  65. $this->field->setUri($this->info['uri']);
  66. $this->field->setNotify($this->createNotify($field));
  67. $setting = Dever::db('api_setting', 'api')->select(array('api_id' => $this->info['id']));
  68. if ($setting) {
  69. foreach ($setting as $k => $v) {
  70. $this->field->set($v['key'], $this->field->value($v['value'], $v['type']));
  71. }
  72. }
  73. }
  74. # 发起请求
  75. protected function curl($url = false)
  76. {
  77. # 生成请求ID
  78. $this->field->createRequestId();
  79. if (!$url) {
  80. $url = $this->url();
  81. }
  82. $method = $this->method();
  83. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  84. $body = $request->body();
  85. $header = $request->header();
  86. $json = $this->info['post_method'] == 3 ? true : false;
  87. $curl = Dever::curl($url, $body, $method, $json, $header);
  88. $curl->setResultHeader(true);
  89. $response_body = $curl->result();
  90. $response_header = $curl->header();
  91. $response_config = array('id' => $this->platform['id'], 'type' => $this->platform['response_type']);
  92. $response = new Response($response_body, $response_header, $this->field, $response_config, $this->type, $this->info['id']);
  93. $result = $response->out();
  94. $log = array();
  95. $log['platform_id'] = $this->platform['id'];
  96. $log['api_id'] = $this->info['id'];
  97. $log['account_id'] = $this->info['account_id'];
  98. $log['account_project'] = $this->info['account_project'];
  99. $log['request_id'] = $this->field->request_id;
  100. $log['url'] = $url;
  101. $log['method'] = $method;
  102. $log['body'] = $body;
  103. $log['header'] = $header;
  104. $log['response_body'] = $response_body;
  105. $log['response_header'] = $response_header;
  106. $log['data'] = $result;
  107. Dever::db('api_log', 'api')->update($log['request_id'], $log);
  108. if ($this->field->log) {
  109. Dever::log($log, 'api');
  110. Dever::debug($log, 'api');
  111. }
  112. return $result;
  113. }
  114. # 直接跳转
  115. protected function location($url = false)
  116. {
  117. if (!$url) {
  118. $url = $this->url();
  119. }
  120. $method = $this->method();
  121. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  122. $request->body();
  123. $url .= '?';
  124. foreach ($this->field->body as $k => $v) {
  125. if ($k == '#') {
  126. $url .= $k . $v;
  127. } else {
  128. $url .= $k . '=' . $v . '&';
  129. }
  130. }
  131. header('Location: '. $url);
  132. }
  133. protected function method()
  134. {
  135. $method = 'get';
  136. if ($this->info['post_method'] == 2) {
  137. $method = 'file';
  138. } elseif ($this->info['method'] == 2) {
  139. $method = 'post';
  140. }
  141. $this->field->setMethod($method);
  142. return $method;
  143. }
  144. protected function url()
  145. {
  146. if (strstr($this->info['uri'], 'http')) {
  147. $this->platform['host'] = '';
  148. }
  149. $path = Dever::db('api_path', 'api')->select(array('api_id' => $this->info['id']));
  150. if ($path) {
  151. $path = array();
  152. foreach ($path as $k => $v) {
  153. $v['value'] = $this->field->value($v['value']);
  154. if ($v['type'] == 1) {
  155. $path[] = $v['value'];
  156. } elseif ($v['type'] == 2) {
  157. $path[] = $v['key'] . '/' . $v['value'];
  158. } elseif ($v['type'] == 3) {
  159. $path[] = $v['key'] . '=' . $v['value'];
  160. }
  161. }
  162. if ($path) {
  163. $path = implode('/', $path);
  164. $this->field->setPath($path);
  165. $this->info['uri'] .= $path;
  166. }
  167. }
  168. $query = Dever::db('api_query', 'api')->select(array('api_id' => $this->info['id']));
  169. if ($query) {
  170. $param = array();
  171. foreach ($query as $k => $v) {
  172. $param[$v['key']] = $this->field->value($v['value'], $v['type']);
  173. if (is_array($param[$v['key']])) {
  174. $param[$v['key']] = Dever::json_encode($param[$v['key']]);
  175. }
  176. }
  177. if ($param) {
  178. $this->field->setQuery($param);
  179. $this->info['uri'] .= '?' . http_build_query($param);
  180. }
  181. }
  182. $url = $this->platform['host'] . $this->info['uri'];
  183. $this->field->setUrl($url);
  184. return $url;
  185. }
  186. }