/** * 网页编码与程序编码转化 * ie:页面提交编码(conf文件中定义,cgi传递,自动判断) de:程序需要的编码(conf文件中定义) * 编码标识以lib库中的编码转化转化函数一致(php手册中的iconv编码) * @param array &$value * @param cgi传过来的ie值 $strCgiIe * @return bool */ private static function __ieTode(&$value, $strCgiIe) { $safConf = Bd_Conf::getAppConf('/saf'); if (empty($safConf)) { $safConf = Bd_Conf::getConf('/saf'); } if ($strCgiIe == '') { $strCgiIe = $safConf['cgi_ie']; } $strConfOe = $safConf['cgi_oe']; if ($strCgiIe == '') { if (true === Bd_Charset::isUtf8(self::__arrToString($value))) { $strCgiIe = 'UTF-8'; } elseif (true === Bd_Charset::isGbk(self::__arrToString($value))) { $strCgiIe = 'GBK'; } } if ($strCgiIe == $strConfOe || $strConfOe == '' || $strCgiIe == '') { return true; } $ret = Bd_String::iconv_recursive($value, $strCgiIe, $strConfOe); if ($ret) { $value = $ret; } return true; }
/** * @brief 统一调用接口 * * @param [in/out] $strService : string 即App,表示api调用的是哪个app的接口 * @param [in/out] $strMethod : string 即App的接口 * @param [in/out] $arrParams : 输入参数 * @param [in/out] $arrFilter : array 指定返回的keys,默认null为全部返回。 * @param [in/out] $extra : array 指定输入的参数的编码,输出编码将和输入编码一致 * @return 返回值: array() or false; //正确执行,则返回数据,格式同接口表述,若错误则返回false * @retval * @see * @note * @author wiki * @date 2011/09/06 16:24:49 **/ public static function call($strService, $strMethod, $arrParams, $arrFilter = null, $extra = null) { //判断strService是否存在 //servicename是驼峰形式 $apiLib = Bd_Conf::getConf("/saf"); $strPrefix = $apiLib['api_lib']; $strServiceClass = $strPrefix . "_Api_" . ucwords($strService) . "_Service"; if (!class_exists($strServiceClass)) { //是否是service调用 $strServiceClass = $strPrefix . "_Service_" . ucwords($strService); if (class_exists($strServiceClass)) { return self::callService($strServiceClass, $strMethod, $arrParams, $arrFilter, $extra); } else { self::$errMsg = "service or api {$strService} not exist"; self::$errNo = self::SERVICE_NOT_EXIST; return false; } } //判断strMethod是否存在 $objService = new $strServiceClass(); if (!method_exists($objService, $strMethod)) { self::$errNo = self::METHOD_NOT_EXIST; self::$errMsg = "class {$strServiceClass} method {$strMethod} not exist"; return false; } //输入参数编码格式转换成service内部的编码格式 if (is_array($extra) && isset($extra['ie']) && $objService->getServieEncode() != strtolower($extra['ie'])) { $arrParams = Bd_String::iconv_recursive($arrParams, strtolower($extra['ie']), $objService->getServieEncode()); } else { if ($extra !== null && (!is_array($extra) || !isset($extra['ie']))) { self::$errNo = self::EXTRA_INVALID; self::$errMsg = "{$strService}::{$strMethod} failed with invalid extra"; return false; } } //输入格式检查 $strMethodReqClass = $strPrefix . "_Api_" . ucwords($strService) . "_Entity_Req" . $strMethod; $strMethodResClass = $strPrefix . "_Api_" . ucwords($strService) . "_Entity_Res" . $strMethod; if (!class_exists($strMethodReqClass)) { self::$errNo = self::INPUT_INVALID; self::$errMsg = "class {$strMethodReqClass} not exist"; return false; } if (!class_exists($strMethodResClass)) { self::$errNo = self::OUTPUT_CHANGED; self::$errMsg = "class {$strMethodResClass} not exist"; return false; } $objMethodReq = new $strMethodReqClass(); $res = $objMethodReq->loadFromArray($arrParams); if (!$res) { self::$errNo = self::INPUT_INVALID; self::$errMsg = "{$strService}::{$strMethod} failed with invalid Input"; return false; } $objMethodRes = new $strMethodResClass(); $res = call_user_func_array(array($objService, $strMethod), array($objMethodReq, $objMethodRes, $arrFilter, $extra)); if ($res === false) { if ($objService->errno != null) { self::$serviceErr = $objService->errno; } else { self::$serviceErr = array(); } self::$errNo = self::METHOD_FAILED; self::$errMsg = "{$strService}::{$strMethod} failed in process"; return false; } if ($res == null) { self::$errNo = self::OUTPUT_CHANGED; self::$errMsg = "{$strService}::{$strMethod} failed with changed output"; return false; } $arrRes = $res->toArray(); //返回值过滤 if (is_array($arrFilter)) { foreach ($arrFilter as $key) { if (isset($arrRes[$key])) { unset($arrRes[$key]); } } } //Service返回值的编码格式和应用客户端编码格式不一致,则转换为api输入的编码格式 if (is_array($extra) && isset($extra['ie']) && $objService->getServieEncode() != strtolower($extra['ie'])) { $arrRes = Bd_String::iconv_recursive($arrRes, $objService->getServieEncode(), $extra['ie']); } //$arrRes = $objMethodRes->toArray(); //$objMethodRes->toArray(); //输出格式要求 return $arrRes; }
public function execute($arrInput, $arrOutput, $strPageService = null, $strUrl = null, $strHttpType = 'post') { $arrParams = $arrInput; if ($this->isLocalService() && $strPageService != null) { /**< 支持本地php调用 */ try { //传参数 autoload切换 $arrParams['app'] = $this->service; $this->setServicePath(array($strHttpType => $arrParams)); //直接调用PS->execute(); $objPS = new $strPageService($arrParams); $arrRes = $objPS->execute(); //autoload切换回来 $this->resetServicePath(); } catch (Exception $e) { $this->resetServicePath(); Bd_Log::warning($e->getMessage(), $e->getCode(), $arrParams); return false; } } else { /* *每个method硬编码一个URl和一个PageService */ if ($strUrl == null) { $strUrl = $this->defaultUrl; } if (strpos($strUrl, "http://") !== false) { /* $objUtil = new Saf_Api_Util_Http(); $strUrl = $objUtil->getUrl($strUrl, $arrParams, $strHttpType); $strRes = $objUtil->fetchUrl($strUrl, $strHttpType, $arrParams, $this->cookie); */ Bd_Log::warning("Url {$strUrl} not valid"); } else { $arrInput = $this->getRalInfo($strUrl, $arrParams, $strHttpType); $strRes = $this->_ralExecute($arrInput); } $arrRes = json_decode($strRes, true); if (strtolower($this->oe) != "utf8" && strtolower($this->oe) != "utf-8") { $arrRes = Bd_String::iconv_recursive($arrRes, "UTF-8", "GBK"); } } if (isset($arrRes['errno']) && intval($arrRes['errno']) != 0) { //业务级错误 $this->errno = $arrRes['errno']; return false; } return $arrRes; }