|
@@ -1,7 +1,6 @@
|
|
<?php namespace Api\Lib;
|
|
<?php namespace Api\Lib;
|
|
use Dever;
|
|
use Dever;
|
|
use Api\Lib\Platform\Field;
|
|
use Api\Lib\Platform\Field;
|
|
-use Api\Lib\Platform\Sign;
|
|
|
|
use Api\Lib\Platform\Ssl;
|
|
use Api\Lib\Platform\Ssl;
|
|
use Api\Lib\Platform\Request;
|
|
use Api\Lib\Platform\Request;
|
|
use Api\Lib\Platform\Response;
|
|
use Api\Lib\Platform\Response;
|
|
@@ -33,17 +32,6 @@ class Platform
|
|
if (!$this->platform) {
|
|
if (!$this->platform) {
|
|
Dever::error('platform error');
|
|
Dever::error('platform error');
|
|
}
|
|
}
|
|
- $this->sign = $this->response = array('id' => $this->platform['id']);
|
|
|
|
- foreach ($this->platform as $k => $v) {
|
|
|
|
- if (strstr($k, 'sign_')) {
|
|
|
|
- $k = str_replace('sign_', '', $k);
|
|
|
|
- $this->sign[$k] = $v;
|
|
|
|
- }
|
|
|
|
- if (strstr($k, 'response_')) {
|
|
|
|
- $k = str_replace('response_', '', $k);
|
|
|
|
- $this->response[$k] = $v;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
if ($this->info['method'] == -1) {
|
|
if ($this->info['method'] == -1) {
|
|
$this->info['method'] = $this->platform['method'];
|
|
$this->info['method'] = $this->platform['method'];
|
|
$this->info['post_method'] = $this->platform['post_method'];
|
|
$this->info['post_method'] = $this->platform['post_method'];
|
|
@@ -56,6 +44,8 @@ class Platform
|
|
protected function field($field)
|
|
protected function field($field)
|
|
{
|
|
{
|
|
if (isset($field['account_project']) && isset($field['account_id'])) {
|
|
if (isset($field['account_project']) && isset($field['account_id'])) {
|
|
|
|
+ $this->info['account_id'] = $field['account_id'];
|
|
|
|
+ $this->info['account_project'] = $field['account_project'];
|
|
$setting = Dever::db('account_setting', $field['account_project'])->select(array('account_id' => $field['account_id']));
|
|
$setting = Dever::db('account_setting', $field['account_project'])->select(array('account_id' => $field['account_id']));
|
|
if ($setting) {
|
|
if ($setting) {
|
|
foreach ($setting as $k => $v) {
|
|
foreach ($setting as $k => $v) {
|
|
@@ -69,51 +59,62 @@ class Platform
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ Dever::error('account error');
|
|
}
|
|
}
|
|
$this->field = new Field($field);
|
|
$this->field = new Field($field);
|
|
|
|
+ $this->field->setPlatformId($this->platform['id']);
|
|
|
|
+ $this->field->setApid($this->info['id']);
|
|
$this->field->setHost($this->platform['host']);
|
|
$this->field->setHost($this->platform['host']);
|
|
$this->field->setUri($this->info['uri']);
|
|
$this->field->setUri($this->info['uri']);
|
|
$this->field->setNotify($this->createNotify($field));
|
|
$this->field->setNotify($this->createNotify($field));
|
|
|
|
+
|
|
|
|
+ $setting = Dever::db('api_setting', 'api')->select(array('api_id' => $this->info['id']));
|
|
|
|
+ if ($setting) {
|
|
|
|
+ foreach ($setting as $k => $v) {
|
|
|
|
+ $this->field->set($v['key'], $this->field->value($v['value'], $v['type']));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
# 发起请求
|
|
# 发起请求
|
|
protected function curl($url = false)
|
|
protected function curl($url = false)
|
|
{
|
|
{
|
|
|
|
+ # 生成请求ID
|
|
|
|
+ $this->field->createRequestId();
|
|
if (!$url) {
|
|
if (!$url) {
|
|
$url = $this->url();
|
|
$url = $this->url();
|
|
}
|
|
}
|
|
$method = $this->method();
|
|
$method = $this->method();
|
|
$request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
|
|
$request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
|
|
- $request_body = $request->body();
|
|
|
|
- if ($this->info['sign_col']) {
|
|
|
|
- $this->platform['sign_col'] = $this->info['sign_col'];
|
|
|
|
- }
|
|
|
|
- $sign = new Sign($this->field, $this->sign);
|
|
|
|
- $sign = $sign->get();
|
|
|
|
-
|
|
|
|
- if ($this->platform['sign_name']) {
|
|
|
|
- $request_body[$this->platform['sign_name']] = $sign;
|
|
|
|
- }
|
|
|
|
- $request_header = $request->header();
|
|
|
|
-
|
|
|
|
|
|
+ $body = $request->body();
|
|
|
|
+ $header = $request->header();
|
|
$json = $this->info['post_method'] == 3 ? true : false;
|
|
$json = $this->info['post_method'] == 3 ? true : false;
|
|
- $curl = Dever::curl($url, $request_body, $method, $json, $request_header);
|
|
|
|
|
|
+ $curl = Dever::curl($url, $body, $method, $json, $header);
|
|
$curl->setResultHeader(true);
|
|
$curl->setResultHeader(true);
|
|
$response_body = $curl->result();
|
|
$response_body = $curl->result();
|
|
$response_header = $curl->header();
|
|
$response_header = $curl->header();
|
|
- $response = new Response($response_body, $response_header, $this->response, $this->field, $this->type, $this->info['id'], $this->sign);
|
|
|
|
|
|
+ $response_config = array('id' => $this->platform['id'], 'type' => $this->platform['response_type']);
|
|
|
|
+ $response = new Response($response_body, $response_header, $this->field, $response_config, $this->type, $this->info['id']);
|
|
$result = $response->out();
|
|
$result = $response->out();
|
|
|
|
+ $log = array();
|
|
$log['platform_id'] = $this->platform['id'];
|
|
$log['platform_id'] = $this->platform['id'];
|
|
- $log['type'] = $this->type;
|
|
|
|
- $log['type_id'] = $this->info['id'];
|
|
|
|
|
|
+ $log['api_id'] = $this->info['id'];
|
|
|
|
+ $log['account_id'] = $this->info['account_id'];
|
|
|
|
+ $log['account_project'] = $this->info['account_project'];
|
|
|
|
+ $log['request_id'] = $this->field->request_id;
|
|
$log['url'] = $url;
|
|
$log['url'] = $url;
|
|
$log['method'] = $method;
|
|
$log['method'] = $method;
|
|
- $log['json'] = $json;
|
|
|
|
- $log['request_body'] = Dever::json_encode($request_body);
|
|
|
|
- $log['request_header'] = Dever::json_encode($request_header);
|
|
|
|
|
|
+ $log['body'] = $body;
|
|
|
|
+ $log['header'] = $header;
|
|
$log['response_body'] = $response_body;
|
|
$log['response_body'] = $response_body;
|
|
- $log['response_header'] = Dever::json_encode($response_header);
|
|
|
|
- Dever::debug($log);
|
|
|
|
|
|
+ $log['response_header'] = $response_header;
|
|
|
|
+ $log['data'] = $result;
|
|
|
|
+ Dever::db('api_log', 'api')->update($log['request_id'], $log);
|
|
|
|
+ if ($this->field->log) {
|
|
|
|
+ Dever::log($log, 'api');
|
|
|
|
+ Dever::debug($log, 'api');
|
|
|
|
+ }
|
|
return $result;
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -125,18 +126,9 @@ class Platform
|
|
}
|
|
}
|
|
$method = $this->method();
|
|
$method = $this->method();
|
|
$request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
|
|
$request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
|
|
- $request_body = $request->body();
|
|
|
|
- if ($this->info['sign_col']) {
|
|
|
|
- $this->platform['sign_col'] = $this->info['sign_col'];
|
|
|
|
- }
|
|
|
|
- $sign = new Sign($this->field, $this->sign);
|
|
|
|
- $sign = $sign->get();
|
|
|
|
-
|
|
|
|
- if ($this->platform['sign_name']) {
|
|
|
|
- $request_body[$this->platform['sign_name']] = $sign;
|
|
|
|
- }
|
|
|
|
|
|
+ $request->body();
|
|
$url .= '?';
|
|
$url .= '?';
|
|
- foreach ($request_body as $k => $v) {
|
|
|
|
|
|
+ foreach ($this->field->body as $k => $v) {
|
|
if ($k == '#') {
|
|
if ($k == '#') {
|
|
$url .= $k . $v;
|
|
$url .= $k . $v;
|
|
} else {
|
|
} else {
|
|
@@ -163,11 +155,10 @@ class Platform
|
|
if (strstr($this->info['uri'], 'http')) {
|
|
if (strstr($this->info['uri'], 'http')) {
|
|
$this->platform['host'] = '';
|
|
$this->platform['host'] = '';
|
|
}
|
|
}
|
|
- $uri = Dever::db('api_uri', 'api')->select(array('api_id' => $this->info['id']));
|
|
|
|
- if ($uri) {
|
|
|
|
|
|
+ $path = Dever::db('api_path', 'api')->select(array('api_id' => $this->info['id']));
|
|
|
|
+ if ($path) {
|
|
$path = array();
|
|
$path = array();
|
|
- $param = array();
|
|
|
|
- foreach ($uri as $k => $v) {
|
|
|
|
|
|
+ foreach ($path as $k => $v) {
|
|
$v['value'] = $this->field->value($v['value']);
|
|
$v['value'] = $this->field->value($v['value']);
|
|
if ($v['type'] == 1) {
|
|
if ($v['type'] == 1) {
|
|
$path[] = $v['value'];
|
|
$path[] = $v['value'];
|
|
@@ -175,23 +166,31 @@ class Platform
|
|
$path[] = $v['key'] . '/' . $v['value'];
|
|
$path[] = $v['key'] . '/' . $v['value'];
|
|
} elseif ($v['type'] == 3) {
|
|
} elseif ($v['type'] == 3) {
|
|
$path[] = $v['key'] . '=' . $v['value'];
|
|
$path[] = $v['key'] . '=' . $v['value'];
|
|
- } elseif ($v['type'] == 4) {
|
|
|
|
- $param[] = $v['value'];
|
|
|
|
- } elseif ($v['type'] == 5) {
|
|
|
|
- $param[] = $v['key'] . '=' . $v['value'];
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if ($path) {
|
|
if ($path) {
|
|
- $this->info['uri'] .= implode('/', $path);
|
|
|
|
|
|
+ $path = implode('/', $path);
|
|
|
|
+ $this->field->setPath($path);
|
|
|
|
+ $this->info['uri'] .= $path;
|
|
}
|
|
}
|
|
- if ($param) {
|
|
|
|
- if (!strstr($this->info['uri'], '?')) {
|
|
|
|
- $this->info['uri'] .= '?';
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $query = Dever::db('api_query', 'api')->select(array('api_id' => $this->info['id']));
|
|
|
|
+ if ($query) {
|
|
|
|
+ $param = array();
|
|
|
|
+ foreach ($query as $k => $v) {
|
|
|
|
+ $param[$v['key']] = $this->field->value($v['value'], $v['type']);
|
|
|
|
+ if (is_array($param[$v['key']])) {
|
|
|
|
+ $param[$v['key']] = Dever::json_encode($param[$v['key']]);
|
|
}
|
|
}
|
|
- $this->info['uri'] .= implode('&', $param);
|
|
|
|
|
|
+ }
|
|
|
|
+ if ($param) {
|
|
|
|
+ $this->field->setQuery($param);
|
|
|
|
+ $this->info['uri'] .= '?' . http_build_query($param);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- $this->field->setUri($this->info['uri']);
|
|
|
|
- return $this->platform['host'] . $this->info['uri'];
|
|
|
|
|
|
+ $url = $this->platform['host'] . $this->info['uri'];
|
|
|
|
+ $this->field->setUrl($url);
|
|
|
|
+ return $url;
|
|
}
|
|
}
|
|
}
|
|
}
|