Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.50 KB | None | 0 0
  1. public function getData($data = [])
  2. {
  3. if ($this->encoding != 'utf-8') {
  4. $login = iconv('utf-8', $this->encoding . '//IGNORE', $this->login);
  5. $password = iconv('utf-8', $this->encoding . '//IGNORE', $this->password);
  6. } else {
  7. $login = $this->login;
  8. $password = $this->password;
  9. }
  10.  
  11. $get = [
  12. 'exportOff' => '1',
  13. 'sort___search_results_by' => 'final_price',
  14. 'article' => $this->code,
  15. 'user' => $login,
  16. 'password' => $password
  17. ];
  18.  
  19. //отключение поиска аналогов при выключенной опции или предпроценке
  20. if ($this->getOption('analogs') != 'Y' || $this->preOrderSearch) {
  21. $get['smode'] = 'A0';
  22. }
  23.  
  24. if ($this->isTestMode()) {
  25. $get['smode'] = 'B0';
  26. }
  27.  
  28. $get = array_merge($get, $data);
  29.  
  30. // для отладки
  31. $parseUrl = [];
  32.  
  33. if (!(parse_url($this->host, PHP_URL_SCHEME))) {
  34. $parseUrl['scheme'] = 'http';
  35. $parseUrl['host'] = $this->host;
  36. } else {
  37. $parseUrl = parse_url($this->host);
  38. }
  39.  
  40. $this->query_url = $parseUrl['scheme'] . '://' . $this->warUser . ':' . $this->warPassword . '@' . $parseUrl['host'] . '/service/search/?' . http_build_query($get);
  41.  
  42. if (isset($this->request->useCache) && $this->request->useCache == false) {
  43. $res = false;
  44. } else {
  45. $res = $this->cache->get($this->query_url);
  46. // http://support.tradesoft.ru/WorkOrder.do?woMode=viewW..
  47. if (stripos($res, '503 Service Temporarily Unavailable')) {
  48. $this->cache->set($this->query_url, null);
  49. $res = false;
  50. }
  51. }
  52.  
  53. if ($res === false || $this->preOrderSearch) {
  54.  
  55. if ($this->limitEnabled) {
  56. $key = md5($this->providerName . ':' . $this->login);
  57. $comment = $this->providerName . ':' . $this->login;
  58. $accessDeny = $this->isLimitReached($key, $this->queryLimit, 3600, $comment);
  59.  
  60. if ($accessDeny) {
  61. $this->error('Доступ временно заблокирован.');
  62. }
  63. }
  64.  
  65. $url = $this->host . '/service/search/?' . http_build_query($get);
  66.  
  67. $httpRequest = new HttpRequest();
  68. $httpRequest->setUrl($url);
  69. $httpRequest->addHeader('Expect:');
  70. $httpRequest->setExtra([
  71. $this,
  72. ]);
  73.  
  74. $curlOptionList = [
  75. CURLOPT_TIMEOUT => 50,
  76. CURLOPT_CONNECTTIMEOUT => 3,
  77. CURLOPT_DNS_CACHE_TIMEOUT => 86400,
  78. ];
  79.  
  80. if ($this->compression) {
  81. $curlOptionList[CURLOPT_ENCODING] = 'gzip';
  82. }
  83.  
  84. if (!empty($this->warUser) && !empty($this->warPassword)) {
  85. $curlOptionList[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
  86. $curlOptionList[CURLOPT_USERPWD] = $this->warUser . ':' . $this->warPassword;
  87. }
  88.  
  89. $httpClient = $this->di->get('httpClient');
  90. $httpClient->addCurlOptionList($curlOptionList);
  91.  
  92. $httpResponse = $httpClient->execute($httpRequest);
  93. $body = $httpResponse->getBody();
  94. $header = $httpResponse->getHeader();
  95. $httpCode = $httpResponse->getCode();
  96.  
  97. $res = $header . "\r\n\r\n" . $body;
  98.  
  99. $errno = $httpResponse->getExtra('curlErrNo');
  100. $info = $httpResponse->getExtra('curlInfo');
  101.  
  102. if ($errno != CURLE_OK) {
  103. $errorMessage = Curl::getErrorMessage($errno);
  104. $this->errorProvider($errorMessage);
  105. }
  106.  
  107. if ($httpCode >= 400) {
  108. $errorMessage = HttpCodes::getDescriptionByCode($httpCode, true);
  109. $this->errorProvider($errorMessage);
  110. }
  111.  
  112. $this->checkError($res);
  113.  
  114. if ($info['size_download'] == 8) {
  115. return [];
  116. }
  117.  
  118. $this->cache->set($this->query_url, $res, 3600);
  119. }
  120.  
  121. if (preg_match('#PHPSESSID=(.*);#i', $res, $PHPSESSID) > 0) {
  122. $this->userData = $PHPSESSID[1];
  123. } else {
  124. $this->userData = null;
  125. }
  126.  
  127. $res = substr($res, strpos($res, "\r\n\r\n") + 4);
  128.  
  129. if (stripos($res, 'Превышено количество одновременных подключений к базе данных')) {
  130. $this->errorProvider('Превышено количество одновременных подключений к базе данных');
  131. }
  132.  
  133. try {
  134. $res_array = [];
  135. $res = base64_decode($res);
  136.  
  137. if (Parse::isSerialized($res)) {
  138. $res_array = unserialize($res)
  139. }
  140.  
  141. $res_array = @unserialize(base64_decode($res));
  142.  
  143. } catch (\Exception $e) {
  144. $context = [
  145. 'text' => $res,
  146. 'url' => !empty($url) ? $url : '',
  147. 'code' => $this->code,
  148. 'login' => $this->login,
  149. ];
  150. $this->di->get('logger')->addDebug($this->name, $context);
  151.  
  152. throw $e;
  153. }
  154.  
  155. if ($res_array === false) {
  156. $context = [
  157. 'code' => $this->code,
  158. 'brand' => $this->producer,
  159. 'get' => http_build_query($get),
  160. 'request' => $this->query_url,
  161. 'responce' => base64_encode($res),
  162. ];
  163.  
  164. if (strlen($res) <= 256) {
  165. $context['res'] = $res;
  166. }
  167.  
  168. $this->di->get('logger')->addDebug($this->host, $context);
  169. $this->errorProvider();
  170. }
  171.  
  172. if (!is_array($res_array)) {
  173. $this->cache->set($this->query_url, false);
  174. $this->errorProvider($errno);
  175. }
  176.  
  177. return $res_array;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement