Example #1
0
 public function execute(RequestInterface $request, $session = null, $bestUrl = null)
 {
     $result = new ResultSet();
     if ($this->checkRequest) {
         try {
             $request->check();
         } catch (TaoBaoSDKException $e) {
             $result->code = $e->getCode();
             $result->msg = $e->getMessage();
             return $result;
         }
     }
     //组装系统参数
     $sysParams["app_key"] = $this->appKey;
     $sysParams["v"] = self::API_VERSION;
     $sysParams["format"] = $this->format;
     $sysParams["sign_method"] = $this->signMethod;
     $sysParams["method"] = $request->getApiMethodName();
     $sysParams["timestamp"] = date("Y-m-d H:i:s");
     if (null != $session) {
         $sysParams["session"] = $session;
     }
     $apiParams = [];
     //获取业务参数
     $apiParams = $request->getApiParas();
     //系统参数放入GET请求串
     if ($bestUrl) {
         $requestUrl = $bestUrl . "?";
         $sysParams["partner_id"] = $this->getClusterTag();
     } else {
         $requestUrl = self::GATEWAY_URL . "?";
         $sysParams["partner_id"] = self::SDK_VERSION;
     }
     // 是否有文件上传
     $fileFields = [];
     foreach ($apiParams as $key => $value) {
         if (is_array($value) && array_key_exists('type', $value) && array_key_exists('content', $value)) {
             $value['name'] = $key;
             $fileFields[$key] = $value;
             unset($apiParams[$key]);
         }
         if (is_numeric($value)) {
             $apiParams[$key] = strval($value);
         }
     }
     // 签名
     $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
     $requestUrl .= http_build_query($sysParams);
     //发起HTTP请求
     try {
         if (count($fileFields) > 0) {
             $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields);
         } else {
             $resp = $this->curl($requestUrl, $apiParams);
         }
     } catch (TaoBaoSDKException $e) {
         $this->logCommunicationError($sysParams["method"], $requestUrl, "HTTP_ERROR_" . $e->getCode(), $e->getMessage());
         $result->code = $e->getCode();
         $result->msg = $e->getMessage();
         return $result;
     }
     unset($apiParams);
     unset($fileFields);
     //解析TOP返回结果
     $respWellFormed = false;
     $respObject = '';
     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 TopLogger();
         $logger->conf["log_file"] = $this->config->getLogFilePath();
         $logger->log([date("Y-m-d H:i:s"), $resp]);
     }
     return $respObject;
 }