public function request($url, $getData = null, $postData = null, $asyncParams = null, $cacheFileName = null) { if ($cacheFileName) { self::$saveCache = strpos($url, 'vsespo') === false; $cachePath = Yii::getPathOfAlias('cacheStorage'); $cacheSubDir = md5($cacheFileName); $cacheSubDir = substr($cacheSubDir, -3); if (!is_dir($cachePath)) { mkdir($cachePath); } if (!file_exists($cachePath . '/' . $cacheSubDir)) { mkdir($cachePath . '/' . $cacheSubDir); } //$cacheFilePath = $cachePath . '/' . $cacheFileName . '.xml'; if (file_exists($cachePath . '/' . $cacheFileName . '.xml') && !file_exists($cachePath . '/' . $cacheSubDir . '/' . $cacheFileName . '.xml')) { rename($cachePath . '/' . $cacheFileName . '.xml', $cachePath . '/' . $cacheSubDir . '/' . $cacheFileName . '.xml'); } $cacheFilePath = $cachePath . '/' . $cacheSubDir . '/' . $cacheFileName . '.xml'; self::$cacheFilePath = $cacheFilePath; self::$downloadExternal = $asyncParams ? false : true; if (file_exists($cacheFilePath) && (!self::$updateProcess || self::$updateProcess && filectime($cacheFilePath) + 3600 * 24 * 14 > time())) { $cacheResult = file_get_contents($cacheFilePath); self::$saveCache = false; } else { if (self::$downCountCacheFill > 0) { self::$downCountCacheFill--; } else { return false; } $cacheResult = false; } } else { $cacheResult = false; self::$cacheFilePath = false; } if (!$cacheResult) { $rCh = curl_init(); if ($postData) { curl_setopt($rCh, CURLOPT_POST, true); } curl_setopt($rCh, CURLOPT_HEADER, true); curl_setopt($rCh, CURLOPT_RETURNTRANSFER, true); if ($postData) { curl_setopt($rCh, CURLOPT_POSTFIELDS, $postData); } curl_setopt($rCh, CURLOPT_TIMEOUT, 80); //$aHeadersToSend = array(); //$aHeadersToSend[] = "Content-Length: " . strlen($sRequest); //$aHeadersToSend[] = "Content-Type: text/xml; charset=utf-8"; //$aHeadersToSend[] = "SOAPAction: \"$sAction\""; //curl_setopt($rCh, CURLOPT_HTTPHEADER, $aHeadersToSend); //evaluate get parametrs if ($getData) { $pos = strpos($url, '?'); if ($pos !== false) { list($url, $args) = explode("?", $url, 2); parse_str($args, $params); $getData = array_merge($params, $getData); } $url = $url . '?' . http_build_query($getData); } curl_setopt($rCh, CURLOPT_URL, $url); $key = $url . md5(serialize($postData)); $mongoKey = substr(md5($key . uniqid('', true)), 0, 10); $hotelRequest = new HotelRequest(); $hotelRequest->requestNum = $mongoKey; self::$requestIds[] = array('key' => $mongoKey, 'class' => get_class($hotelRequest), 'keyName' => 'requestNum'); $hotelRequest->timestamp = time(); //echo 'send req: '.self::$lastRequestMethod."\n"; $hotelRequest->methodName = self::$lastRequestMethod; $hotelRequest->requestUrl = $url; if (self::$groupId) { $hotelRequest->groupId = self::$groupId; } $hotelRequest->requestDescription = self::$lastRequestDescription; $hotelRequest->requestXml = isset($postData['request']) ? $postData['request'] : ''; $valid = $hotelRequest->save(); if (!$valid) { CVarDumper::dump($hotelRequest->getErrors()); } } if ($asyncParams === null) { if (!$cacheResult) { $startTime = microtime(true); $sData = curl_exec($rCh); $endTime = microtime(true); //Biletoid_Utils::addLogMessage($sData, '/tmp/curl_response.txt'); if ($sData !== FALSE) { list($sHeaders, $sData) = explode("\r\n\r\n", $sData, 2); if (strpos($sHeaders, 'Continue') !== FALSE) { list($sHeaders, $sData) = explode("\r\n\r\n", $sData, 2); } $this->lastHeaders = $sHeaders; $hotelRequest->executionTime = $endTime - $startTime; $hotelRequest->responseXml = UtilsHelper::formatXML($sData); $hotelRequest->save(); if ($cacheFileName && self::$saveCache) { file_put_contents($cacheFilePath, $sData); } } else { $this->lastCurlError = curl_error($rCh); $hotelRequest->errorDescription = $this->lastCurlError; $hotelRequest->save(); } unset($url); unset($hotelRequest); return $sData; } else { unset($url); return $cacheResult; } } else { if (!$cacheResult) { curl_setopt($rCh, CURLOPT_HEADER, false); if (!$this->multiCurl) { $this->multiCurl = curl_multi_init(); } $this->requests[] = array('curlHandle' => $rCh, 'completed' => false, 'hotelRequestLog' => $hotelRequest, 'cacheFilePath' => $cacheFileName ? $cacheFilePath : null); $id = count($this->requests) - 1; curl_multi_add_handle($this->multiCurl, $this->requests[$id]['curlHandle']); $this->requests[$id]['id'] = $id; $this->requests[$id] = array_merge($this->requests[$id], $asyncParams); unset($url); return $id; } else { $this->requests[] = array('curlHandle' => null, 'completed' => true, 'hotelRequestLog' => null, 'cacheFilePath' => $cacheFileName ? $cacheFilePath : null); $id = count($this->requests) - 1; $this->requests[$id]['id'] = $id; //$this->requests[$id] = array_merge($this->requests[$id], $asyncParams); if (isset($asyncParams['function'])) { $params = array($cacheResult); if (isset($asyncParams['params']) and $asyncParams['params']) { foreach ($asyncParams['params'] as $param) { $params[] = $param; } } try { $this->requests[$id]['result'] = call_user_func_array($asyncParams['function'], $params); } catch (Exception $e) { Yii::log('HotelBookClient Return Incorrect Response:' . CVarDumper::dumpAsString($asyncParams) . CVarDumper::dumpAsString($params)); unlink($cacheFilePath); } //$this->requests[$id]['result'] = call_user_func_array($asyncParams['function'], $params); } else { $this->requests[$id]['result'] = $cacheResult; } unset($url); return $id; } } }