function __construct($response, $app_id) { $sign = $response['sign']; unset($response['sign']); $this->response = $response; if (!$app_id || !base_shopnode::token($app_id)) { $sign_check = base_certificate::gen_sign($response); } else { $sign_check = base_shopnode::gen_sign($response, $app_id); } if ($sign != $sign_check) { //trigger_error('sign error!',E_USER_ERROR); echo json_encode(array('rsp' => 'fail', 'res' => 4003, 'data' => 'sign error')); exit; } }
function __construct($response, $app_id) { $sign = $response['sign']; unset($response['sign']); $this->response = $response; if (!$app_id || !base_shopnode::token($app_id)) { $sign_check = base_certificate::gen_sign($response); } else { $sign_check = base_shopnode::gen_sign($response, $app_id); } if ('private' == app::get('system')->getConf('system.matrix.set')) { $sign_check = kernel::single('system_shopmatrix')->get_sign($response); } if ($sign != $sign_check) { //trigger_error('sign error!',E_USER_ERROR); echo json_encode(array('rsp' => 'fail', 'res' => 4003, 'data' => 'sign error')); exit; } }
private function parse_rpc_request($request) { $sign = $request['sign']; unset($request['sign']); $app_id = $request['app_id']; if ($app_id) { $app_id = substr($app_id, strpos($app_id, '.') + 1, strlen($app_id)); } if ($request["direct"] == 'true') { $sign_check = base_certificate::gen_sign($request); } else { if (!base_shopnode::token($app_id)) { $sign_check = base_certificate::gen_sign($request); } else { $sign_check = base_shopnode::gen_sign($request, $app_id); } } if ($sign != $sign_check) { //trigger_error('sign error',E_USER_ERROR); $this->send_user_error('4003', 'sign error'); return false; } $system_params = array('app_id', 'method', 'date', 'format', 'certi_id', 'v', 'sign', 'node_id'); foreach ($system_params as $name) { $call[$name] = $request[$name]; unset($request[$name]); } //api version control 20120627 mabaineng $system_params_addon = array('from_node_id', 'from_api_v', 'to_node_id', 'to_api_v'); foreach ($system_params_addon as $name) { if ($request[$name]) { self::$api_info[$name] = $request[$name]; unset($request[$name]); } } //if method request = 'aaa.bbb.ccc.ddd' //then: object_service = api.aaa.bbb.ccc, method=ddd if (isset($call['method'][2])) { if ($p = strrpos($call['method'], '.')) { $service = substr($call['method'], 0, $p); self::$api_info['api_name'] = $service; $service = 'api.' . $service; $method = substr($call['method'], $p + 1); } } else { //trigger_error('error method',E_ERROR); $this->send_user_error('4001', 'error method'); return false; } if ($call['node_id']) { self::$node_id = $call['node_id']; } return array($service, $method, $request); }
public function call($method, $params, $rpc_id = null, $gzip = false) { $api_log = kernel::single('apiactionlog_router_logging'); $rpc_id = $api_log->request_log($method, $params, $rpc_id); if (!$rpc_id) { $microtime = microtime(true); $rpc_id = str_replace('.', '', strval($microtime)); $randval = uniqid('', true); $rpc_id .= strval($randval); $rpc_id = md5($rpc_id); } $headers = array('Connection' => 'Close'); if ($gzip) { $headers['Content-Encoding'] = 'gzip'; } $query_params = array('app_id' => 'ecos.' . $this->app->app_id, 'method' => $method, 'date' => date('Y-m-d H:i:s'), 'callback_url' => kernel::openapi_url('openapi.rpc_callback', 'async_result_handler', array('id' => $rpc_id, 'app_id' => $this->app->app_id)), 'format' => 'json', 'certi_id' => base_certificate::certi_id(), 'v' => $this->api_version($method), 'from_node_id' => base_shopnode::node_id($this->app->app_id)); $query_params = array_merge((array) $params, $query_params); // rpc_id 分id 和 calltime $arr_rpc_key = explode('-', $rpc_id); $rpc_id = $arr_rpc_key[0]; $rpc_calltime = $arr_rpc_key[1]; $query_params['task'] = $rpc_id; if (!base_shopnode::token($this->app->app_id)) { $query_params['sign'] = base_certificate::gen_sign($query_params); } else { $query_params['sign'] = base_shopnode::gen_sign($query_params, $this->app->app_id); } $url = $this->get_url($this->network_id); $core_http = kernel::single('base_httpclient'); $response = $core_http->set_timeout($this->timeout)->post($url, $query_params, $headers); logger::info('Response: ' . $response); if ($response === HTTP_TIME_OUT) { $headers = $core_http->responseHeader; logger::info('Request timeout, process-id is ' . $headers['process-id']); $api_log->update(array('msg_id' => $headers['process-id'], 'status' => 'fail', 'msg' => '请求超时'), $rpc_id, $rpc_calltime); $this->status = RPC_RST_RUNNING; return false; } else { $result = json_decode($response); if ($result) { $this->error = $response->error; switch ($result->rsp) { case 'running': $this->status = RPC_RST_RUNNING; $api_log->update(array('msg_id' => $result->msg_id, 'status' => 'running'), $rpc_id, $rpc_calltime); // 存入中心给的process-id也就是msg-id return true; case 'succ': $result = json_decode($response, true); $api_log->update(array('msg_id' => $result['msg_id'], 'status' => 'success', 'calltime' => time()), $rpc_id, $rpc_calltime); $this->status = RPC_RST_FINISH; $this->rpc_response = $response; return $result['data']; case 'fail': $this->error = 'Bad response'; $this->status = RPC_RST_ERROR; $api_log->update(array('msg_id' => $result->msg_id, 'status' => 'fail', 'msg' => $result->res), $rpc_id, $rpc_calltime); $this->rpc_response = $response; return false; } } else { //error 解码失败 } } }
public function call($method, $params, $rpc_id = null, $gzip = false) { if (is_null($rpc_id)) { $rpc_id = $this->begin_transaction($method, $params); } else { $rpc_id = $this->begin_transaction($method, $params, $rpc_id); } $obj_rpc_poll = app::get('base')->model('rpcpoll'); $headers = array('Connection' => 'Close'); if ($gzip) { $headers['Content-Encoding'] = 'gzip'; } $query_params = array('app_id' => 'ecos.' . $this->app->app_id, 'method' => $method, 'date' => date('Y-m-d H:i:s'), 'callback_url' => kernel::openapi_url('openapi.rpc_callback', 'async_result_handler', array('id' => $rpc_id, 'app_id' => $this->app->app_id)), 'format' => 'json', 'certi_id' => base_certificate::certi_id(), 'v' => $this->api_version($method), 'from_node_id' => base_shopnode::node_id($this->app->app_id)); // rpc_id 分id 和 calltime $arr_rpc_key = explode('-', $rpc_id); $rpc_id = $arr_rpc_key[0]; $rpc_calltime = $arr_rpc_key[1]; $query_params['task'] = $rpc_id; $query_params = array_merge((array) $params, $query_params); if (!base_shopnode::token($this->app->app_id)) { $query_params['sign'] = base_certificate::gen_sign($query_params); } else { $query_params['sign'] = base_shopnode::gen_sign($query_params, $this->app->app_id); } $url = $this->get_url($this->network_id); $core_http = kernel::single('base_httpclient'); $response = $core_http->set_timeout($this->timeout)->post($url, $query_params, $headers); kernel::log('Response: ' . $response); if ($this->callback_class && method_exists(kernel::single($this->callback_class), 'response_log')) { $response_log_func = 'response_log'; $callback_params = $this->callback_params ? array_merge($this->callback_params, array('rpc_key' => $rpc_id . '-' . $rpc_calltime)) : array('rpc_key' => $rpc_id . '-' . $rpc_calltime); kernel::single($this->callback_class)->{$response_log_func}($response, $callback_params); } if ($response === HTTP_TIME_OUT) { $headers = $core_http->responseHeader; kernel::log('Request timeout, process-id is ' . $headers['process-id']); $obj_rpc_poll->update(array('process_id' => $headers['process-id']), array('id' => $rpc_id, 'calltime' => $rpc_calltime, 'type' => 'request')); $this->status = RPC_RST_RUNNING; return false; } else { $result = json_decode($response); if ($result) { $this->error = $response->error; switch ($result->rsp) { case 'running': $this->status = RPC_RST_RUNNING; // 存入中心给的process-id也就是msg-id $obj_rpc_poll->update(array('process_id' => $result->msg_id), array('id' => $rpc_id, 'type' => 'request', 'calltime' => $rpc_calltime)); return true; case 'succ': //$obj_rpc_poll->delete(array('id'=>$rpc_id,'calltime'=>$rpc_calltime,'type'=>'request','fail_times'=>1)); $obj_rpc_poll->delete(array('id' => $rpc_id, 'calltime' => $rpc_calltime, 'type' => 'request', 'fail_times' => 1)); $this->status = RPC_RST_FINISH; $method = $this->callback_method; if ($method && $this->callback_class) { kernel::single($this->callback_class)->{$method}($result->data); } $this->rpc_response = $response; return $result->data; case 'fail': $this->error = 'Bad response'; $this->status = RPC_RST_ERROR; $this->rpc_response = $response; return false; } } else { //error 解码失败 } } }