示例#1
0
 protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt)
 {
     $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI";
     $logger = new LtLogger();
     $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->appkey . "_" . date("Y-m-d") . ".log";
     $logger->conf["separator"] = "^_^";
     $logData = array(date("Y-m-d H:i:s"), $apiName, $this->appkey, $localIp, PHP_OS, $this->sdkVersion, $requestUrl, $errorCode, str_replace("\n", "", $responseTxt));
     $logger->log($logData);
 }
示例#2
0
文件: TopClient.php 项目: honj51/33pu
 public function execute($request, $session = null)
 {
     if ($this->checkRequest) {
         try {
             $request->check();
         } catch (Exception $e) {
             $result->code = $e->getCode();
             $result->msg = $e->getMessage();
             return $result;
         }
     }
     //组装系统参数
     $sysParams["app_key"] = $this->appkey;
     $sysParams["v"] = $this->apiVersion;
     $sysParams["format"] = $this->format;
     $sysParams["sign_method"] = $this->signMethod;
     $sysParams["method"] = $request->getApiMethodName();
     $sysParams["timestamp"] = date("Y-m-d H:i:s");
     $sysParams["partner_id"] = $this->sdkVersion;
     if (null != $session) {
         $sysParams["session"] = $session;
     }
     //获取业务参数
     $apiParams = $request->getApiParas();
     //签名
     $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
     //系统参数放入GET请求串
     $requestUrl = $this->gatewayUrl . "?";
     foreach ($sysParams as $sysParamKey => $sysParamValue) {
         $requestUrl .= "{$sysParamKey}=" . urlencode($sysParamValue) . "&";
     }
     $requestUrl = substr($requestUrl, 0, -1);
     //发起HTTP请求
     try {
         $resp = $this->curl($requestUrl, $apiParams);
     } catch (Exception $e) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_ERROR_" . $e->getCode(), $e->getMessage());
         $result->code = $e->getCode();
         $result->msg = $e->getMessage();
         return $result;
     }
     //解析TOP返回结果
     $respWellFormed = false;
     if ("json" == $this->format) {
         $respObject = json_decode($resp);
         if (null !== $respObject) {
             $respWellFormed = true;
             foreach ($respObject as $propKey => $propValue) {
                 $respObject = $propValue;
             }
         }
     } else {
         if ("xml" == $this->format) {
             $respObject = @simplexml_load_string($resp);
             if (false !== $respObject) {
                 $respWellFormed = true;
             }
         }
     }
     //返回的HTTP文本不是标准JSON或者XML,记下错误日志
     if (false === $respWellFormed) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_RESPONSE_NOT_WELL_FORMED", $resp);
         $result->code = 0;
         $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
         return $result;
     }
     //如果TOP返回了错误码,记录到业务错误日志中
     if (isset($respObject->code)) {
         $logger = new LtLogger();
         $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log";
         $logger->log(array(date("Y-m-d H:i:s"), $resp));
     }
     return $respObject;
 }
示例#3
0
 public function execute($request, $session = null)
 {
     $this->_method = $request->getApiMethodName();
     $this->_CachePath = $this->_CachePath . "runtime/FtxiaAPI/";
     if (!$this->appkey) {
         $result = '{"errors":{"error":{"msg":"Missing App Key","err_code":"10002"}}}';
         $logger = new LtLogger();
         $logger->conf["log_file"] = $this->_CachePath . "ftxia_logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log";
         $logger->log(array(date("Y-m-d H:i:s"), $this->_method, $result));
         return $result;
     }
     if ($this->checkRequest) {
         try {
             $request->check();
         } catch (Exception $e) {
             $result->code = $e->getCode();
             $result->msg = $e->getMessage();
             return $result;
         }
     }
     //组装系统参数
     $sysParams["app_key"] = $this->appkey;
     $sysParams["app_secret"] = $this->secretKey;
     $sysParams["v"] = $this->apiVersion;
     $sysParams["format"] = $this->format;
     $sysParams["sign_method"] = $this->signMethod;
     $sysParams["method"] = $request->getApiMethodName();
     $sysParams["timestamp"] = date("Y-m-d H:i:s");
     $sysParams["dethod"] = $_SERVER['SERVER_NAME'];
     $sysParams["partner_id"] = $this->sdkVersion;
     if (null != $session) {
         $sysParams["session"] = $session;
     }
     //获取业务参数
     $apiParams = $request->getApiParas();
     //签名
     $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
     //系统参数放入GET请求串
     $requestUrl = $this->gatewayUrl . $sysParams['method'] . "?";
     foreach ($sysParams as $sysParamKey => $sysParamValue) {
         $requestUrl .= "{$sysParamKey}=" . urlencode($sysParamValue) . "&";
     }
     $requestUrl = substr($requestUrl, 0, -1);
     $cacheid = md5($this->createStrParam($apiParams));
     if (!($resp = $this->getCacheData($cacheid))) {
         //发起HTTP请求
         $logger = new LtLogger();
         $logger->conf["log_file"] = $this->_CachePath . "ftxia_logs/logs_" . $this->appkey . "_" . date("Y-m-d") . ".log";
         $logger->log(array($this->appkey, $this->_method, date("Y-m-d H:i:s")));
         try {
             $resp = $this->curl($requestUrl, $apiParams);
             if (strlen($resp) > 50) {
                 $this->saveCacheData($cacheid, $resp);
             }
         } catch (Exception $e) {
             $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_ERROR_" . $e->getCode(), $e->getMessage());
             $result->code = $e->getCode();
             $result->msg = $e->getMessage();
             return $result;
         }
     } else {
         if (strlen($resp) > 100) {
             $this->saveCacheData($cacheid, $resp);
         }
     }
     //解析TOP返回结果
     $respWellFormed = false;
     if ("json" == $this->format) {
         $respObject = json_decode($resp);
         if (null !== $respObject) {
             $respWellFormed = true;
             foreach ($respObject as $propKey => $propValue) {
                 $respObject = $propValue;
             }
         }
     } else {
         if ("xml" == $this->format) {
             $respObject = @simplexml_load_string($resp);
             if (false !== $respObject) {
                 $respWellFormed = true;
             }
         }
     }
     //返回的HTTP文本不是标准JSON或者XML,记下错误日志
     if (false === $respWellFormed) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_RESPONSE_NOT_WELL_FORMED", $resp);
         $result->code = 0;
         $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
         return $result;
     }
     //如果TOP返回了错误码,记录到业务错误日志中
     if (isset($respObject->error->err_code)) {
         $logger = new LtLogger();
         $logger->conf["log_file"] = $this->_CachePath . "ftxia_logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log";
         $logger->log(array(date("Y-m-d H:i:s"), $resp));
     }
     return $respObject;
 }
示例#4
0
 public function execute($request, $session = null)
 {
     if ($this->checkRequest) {
         try {
             $request->check();
         } catch (Exception $e) {
             $result->code = $e->getCode();
             $result->msg = $e->getMessage();
             return $result;
         }
     }
     $sysParams["app_key"] = $this->appkey;
     $sysParams["v"] = $this->apiVersion;
     $sysParams["format"] = $this->format;
     $sysParams["sign_method"] = $this->signMethod;
     $sysParams["method"] = $request->getApiMethodName();
     $sysParams["timestamp"] = date("Y-m-d H:i:s");
     $sysParams["partner_id"] = $this->sdkVersion;
     if (null != $session) {
         $sysParams["session"] = $session;
     }
     $apiParams = $request->getApiParas();
     $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
     $requestUrl = $this->gatewayUrl . "?";
     foreach ($sysParams as $sysParamKey => $sysParamValue) {
         $requestUrl .= "{$sysParamKey}=" . urlencode($sysParamValue) . "&";
     }
     $requestUrl = substr($requestUrl, 0, -1);
     $appLog->info("request=" . $requestUrl . "\n");
     $appLog->info("apiParams:");
     foreach ($apiParams as $apiParamKey => $apiParamValue) {
         $appLog->info($apiParamKey . "=" . $apiParamValue);
     }
     try {
         $resp = utf8_decode($this->curl($requestUrl, $apiParams));
         $appLog->info("result = " . $resp . "\n");
     } catch (Exception $e) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_ERROR_" . $e->getCode(), $e->getMessage());
         $result->code = $e->getCode();
         $result->msg = $e->getMessage();
         return $result;
     }
     $respWellFormed = false;
     if ("json" == $this->format) {
         $respObject = json_decode($resp);
         if (null !== $respObject) {
             $respWellFormed = true;
             foreach ($respObject as $propKey => $propValue) {
                 $respObject = $propValue;
             }
         }
     } else {
         if ("xml" == $this->format) {
             $respObject = @simplexml_load_string($resp);
             if (false !== $respObject) {
                 $respWellFormed = true;
             }
         }
     }
     if (false === $respWellFormed) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_RESPONSE_NOT_WELL_FORMED", $resp);
         $result->code = 0;
         $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
         return $result;
     }
     if (isset($respObject->code)) {
         $logger = new LtLogger();
         $logger->log(array(date("Y-m-d H:i:s"), $resp));
     }
     return $respObject;
 }
示例#5
0
 public function execute($request, $authToken = null)
 {
     $sysParams["app_id"] = $this->appId;
     $sysParams["version"] = $this->apiVersion;
     $sysParams["format"] = $this->format;
     $sysParams["sign_type"] = $this->signType;
     $sysParams["method"] = $request->getApiMethodName();
     $sysParams["timestamp"] = date("Y-m-d H:i:s");
     $sysParams["auth_token"] = $authToken;
     $sysParams["alipay_sdk"] = $this->alipaySdkVersion;
     $sysParams["terminal_type"] = $request->getTerminalType();
     $sysParams["terminal_info"] = $request->getTerminalInfo();
     $apiParams = $request->getApiParas();
     $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
     $requestUrl = $this->gatewayUrl . "?";
     foreach ($sysParams as $sysParamKey => $sysParamValue) {
         $requestUrl .= "{$sysParamKey}=" . urlencode($sysParamValue) . "&";
     }
     $requestUrl = substr($requestUrl, 0, -1);
     try {
         $resp = $this->curl($requestUrl, $apiParams);
     } catch (Exception $e) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_ERROR_" . $e->getCode(), $e->getMessage());
         return false;
     }
     $respWellFormed = false;
     if ("json" == $this->format) {
         $respObject = json_decode($resp);
         if (null !== $respObject) {
             $respWellFormed = true;
         }
     } else {
         if ("xml" == $this->format) {
             $respObject = @simplexml_load_string($resp);
             if (false !== $respObject) {
                 $respWellFormed = true;
             }
         }
     }
     if (false === $respWellFormed) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_RESPONSE_NOT_WELL_FORMED", $resp);
         return false;
     }
     if (isset($respObject->code)) {
         $logger = new LtLogger();
         $logger->conf["log_file"] = rtrim(AOP_SDK_WORK_DIR, '\\/') . '/' . "logs/aop_biz_err_" . $this->appId . "_" . date("Y-m-d") . ".log";
         $logger->log(array(date("Y-m-d H:i:s"), $resp));
     }
     return $respObject;
 }
示例#6
0
 public function execute($request, $authToken = null)
 {
     if ($this->checkEmpty($this->postCharset)) {
         $this->postCharset = "UTF-8";
     }
     $this->fileCharset = mb_detect_encoding($this->appId, "UTF-8,GBK");
     //		//  如果两者编码不一致,会出现签名验签或者乱码
     if (strcasecmp($this->fileCharset, $this->postCharset)) {
         // writeLog("本地文件字符集编码与表单提交编码不一致,请务必设置成一样,属性名分别为postCharset!");
         throw new Exception("文件编码:[" . $this->fileCharset . "] 与表单提交编码:[" . $this->postCharset . "]两者不一致!");
     }
     $iv = null;
     if (!$this->checkEmpty($request->getApiVersion())) {
         $iv = $request->getApiVersion();
     } else {
         $iv = $this->apiVersion;
     }
     //组装系统参数
     $sysParams["app_id"] = $this->appId;
     $sysParams["version"] = $iv;
     $sysParams["format"] = $this->format;
     $sysParams["sign_type"] = $this->signType;
     $sysParams["method"] = $request->getApiMethodName();
     $sysParams["timestamp"] = date("Y-m-d H:i:s");
     $sysParams["auth_token"] = $authToken;
     $sysParams["alipay_sdk"] = $this->alipaySdkVersion;
     $sysParams["terminal_type"] = $request->getTerminalType();
     $sysParams["terminal_info"] = $request->getTerminalInfo();
     $sysParams["prod_code"] = $request->getProdCode();
     $sysParams["notify_url"] = $request->getNotifyUrl();
     $sysParams["charset"] = $this->postCharset;
     //获取业务参数
     $apiParams = $request->getApiParas();
     //签名
     $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
     //系统参数放入GET请求串
     $requestUrl = $this->gatewayUrl . "?";
     foreach ($sysParams as $sysParamKey => $sysParamValue) {
         $requestUrl .= "{$sysParamKey}=" . urlencode($this->characet($sysParamValue, $this->postCharset)) . "&";
     }
     $requestUrl = substr($requestUrl, 0, -1);
     // writeLog("requestUrl: " . $requestUrl);
     // writeLog("apiParams: " . var_export($apiParams, true));
     //发起HTTP请求
     try {
         $resp = $this->curl($requestUrl, $apiParams);
     } catch (Exception $e) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_ERROR_" . $e->getCode(), $e->getMessage());
         return false;
     }
     //解析AOP返回结果
     $respWellFormed = false;
     // writeLog("alipay response  content: " . $resp);
     // 将返回结果转换本地文件编码
     $r = iconv($this->postCharset, $this->fileCharset . "//IGNORE", $resp);
     $signData = null;
     if ("json" == $this->format) {
         $respObject = json_decode($r);
         if (null !== $respObject) {
             $respWellFormed = true;
             $signData = $this->parserJSONSignData($request, $resp, $respObject);
         }
     } else {
         if ("xml" == $this->format) {
             $respObject = @simplexml_load_string($resp);
             if (false !== $respObject) {
                 $respWellFormed = true;
                 $signData = $this->parserXMLSignData($request, $resp);
             }
         }
     }
     //返回的HTTP文本不是标准JSON或者XML,记下错误日志
     if (false === $respWellFormed) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_RESPONSE_NOT_WELL_FORMED", $resp);
         return false;
     }
     //如果AOP返回了错误码,记录到业务错误日志中
     if (isset($respObject->sub_code)) {
         $logger = new LtLogger();
         $logger->conf["log_file"] = rtrim(AOP_SDK_WORK_DIR, '\\/') . '/' . "logs/aop_biz_err_" . $this->appId . "_" . date("Y-m-d") . ".log";
         $logger->log(array(date("Y-m-d H:i:s"), $resp));
     }
     // 验签
     if (!$this->checkEmpty($this->alipayPublicKey)) {
         if ($signData == null || $this->checkEmpty($signData->sign) || $this->checkEmpty($signData->signSourceData)) {
             throw new Exception(" check sign Fail! The reason : signData is Empty");
         }
         if (!$this->checkEmpty($respObject->sub_code) || $this->checkEmpty($respObject->sub_code) && !$this->checkEmpty($signData->sign)) {
             $checkResult = $this->verify($signData->signSourceData, $signData->sign, $this->alipayPublicKey);
             if (!$checkResult) {
                 if (strpos($signData->signSourceData, "\\/") > 0) {
                     $signData->signSourceData = str_replace("\\/", "/", $signData->signSourceData);
                     $checkResult = $this->verify($signData->signSourceData, $signData->sign, $this->alipayPublicKey);
                     if (!$checkResult) {
                         throw new Exception("check sign Fail! [sign=" . $signData->sign . ", signSourceData=" . $signData->signSourceData . "]");
                     }
                 } else {
                     throw new Exception("check sign Fail! [sign=" . $signData->sign . ", signSourceData=" . $signData->signSourceData . "]");
                 }
             }
         }
     }
     return $respObject;
 }