public function handle_request() { $this->_apf = APF::get_instance(); $this->_request = $this->_apf->get_request(); $this->_params = $this->_request->get_parameters(); $request_body = file_get_contents('php://input'); if ($request_body) { $res = json_decode($request_body, true); if (is_array($res)) { foreach ($res as $k => $v) { $_REQUEST[$k] = $v; } } } $this->_params = array_merge($this->_params, $_REQUEST); $router_matches = $this->_request->get_router_matches(); try { // 验证参数 if (isset($this->paramRules)) { $validator = new Util_Validator($this->_params, $this->paramRules); if ($validator->fails()) { throw new Exception_General_InvalidParameters(json_encode($validator->messages())); } } // 通过缓存去获取数据 if (self::$_totalCacheOnOff && $this->_isCache && $this->setCacheKey()) { // 获取数据 $mem = APF_Cache_Factory::get_instance()->get_memcache(); if ($this->_params['rmCache']) { // 是否清除缓存 $mem->delete($this->_cacheKey); // 删除缓存 } $ret = $mem->get($this->_cacheKey); if (!$ret) { $ret = $this->handle_request_internal(); if ($ret['status'] == Const_APIStatus::RETURN_CODE_OK) { $mem->set($this->_cacheKey, $ret, $this->_isCacheZip, $this->_cacheTime); } } } else { $ret = $this->handle_request_internal(); } // 生成用户友好的错误消息 if ($ret['status'] == Const_APIStatus::RETURN_CODE_ERROR) { $apiError = new Api_Error($ret['message'], $ret['errcode']); if ($this->isDebugMode()) { $apiError->enableDebugMode(); } $ret = $apiError->toArray(); } } catch (Exception $e) { $apiError = new Api_Error($e); if ($this->isDebugMode()) { $apiError->enableDebugMode(); } $ret = $apiError->toArray(); } $this->output($ret); }
<?php include_once 'inc.common.php'; $request = preg_replace('/^' . preg_quote(SITE_DIR, '/') . '/', '', $_SERVER["REQUEST_URI"]); $url = explode('/', preg_replace('/\\?[^\\/]+$/', '', $request)); $url = array_slice($url, 2); $class = 'Api_' . implode('_', array_map('ucfirst', $url)); if (!class_exists($class)) { $class = 'Api_Error'; } $request = new Api_Request(); $worker = new $class($request); if (!$worker instanceof Api_Abstract) { $worker = new Api_Error($request); } echo $worker->process_request()->send_headers()->get_response();