diff --git a/src/DirectApiService.php b/src/DirectApiService.php index 4b43245..bd2a7d2 100644 --- a/src/DirectApiService.php +++ b/src/DirectApiService.php @@ -432,7 +432,13 @@ public function doRequest(RequestInterface $request, int $try = 0, int $maxTry = throw new DirectApiException('Ошибка при подключении к яндексу: ' . $exception->getMessage() . ' Code: ' . $exception->getCode()); } } catch (RequestException $exception) { - throw new DirectApiException('Ошибка при отправке запроса к яндексу: ' . $exception->getMessage() . '. Response: ' . $exception->getResponse()->getBody()->getContents() . ' Code: ' . $exception->getCode(), 0, null, $exception->getResponse()->getBody()->getContents()); + $response = $exception->getResponse(); + if ($response) { + $response = $response->getBody()->getContents(); + } else { + $response = ""; + } + throw new DirectApiException('Ошибка при отправке запроса к яндексу: ' . $exception->getMessage() . '. Response: ' . $response . ' Code: ' . $exception->getCode(), 0, null, $exception->getResponse()->getBody()->getContents()); } catch (\Throwable $exception) { throw new DirectApiException('Ошибка при запросе к яндексу' . $exception->getMessage() . ' Code: ' . $exception->getCode()); } diff --git a/src/services/reports/ReportsService.php b/src/services/reports/ReportsService.php index e30e3f7..290a514 100644 --- a/src/services/reports/ReportsService.php +++ b/src/services/reports/ReportsService.php @@ -89,9 +89,9 @@ public function getReport( return $this->getReportResponse($this->getRequesJson($reportDefinition), $mode, $returnMoneyInMicros); } - public function getRequesJson(ReportDefinition $reportDefinition) + public function getRequesJson(ReportDefinition $reportDefinition) { - $jsonRequest = ["params"=>$reportDefinition]; + $jsonRequest = ["params" => $reportDefinition]; return json_encode($jsonRequest); } @@ -142,26 +142,19 @@ public function getReportResponse($payload, $mode, $returnMoneyInMicros) $result = null; while ($code !== 200) { try { - $result = $this->service->doRequest($request); - //отправка повторного запросы при пустом ответе - if ($result === null) { - $result = $this->service->doRequest($request); - } + $result = $this->getResult(0, $request); } catch (DirectApiException $ex) { if ($ex->response) { - - $simpleXMLElementResponse = new SimpleXMLElement($ex->response, 0, false, 'http://api.direct.yandex.com/v5/reports'); - $errorCode = (int)$simpleXMLElementResponse->xpath('//reports:errorCode')[0]; - if ($errorCode === 1002) { + $exResp = json_decode($ex->response, true); + $errorCode = (int)$exResp['error']['error_code']; + if ($errorCode === 500) { try { - $result = $this->service->doRequest($request); - //отправка повторного запросы при пустом ответе - if ($result === null) { - $result = $this->service->doRequest($request); - } + $result = $this->getResult(0, $request); } catch (BadResponseException $ex) { $this->badResponseExceptionAnswer($ex); } + } else { + $this->badResponseExceptionAnswer($ex); } } @@ -186,9 +179,28 @@ public function getReportResponse($payload, $mode, $returnMoneyInMicros) return $this->parseReportResponse($body); } + /** + * @param $attemptNumber + * @param $request + * @return \Psr\Http\Message\ResponseInterface + * @throws DirectApiException + */ + private function getResult($attemptNumber, $request) + { + while ($attemptNumber < 100) { + $result = $this->service->doRequest($request); + if ($result === null) { + sleep(1); + $this->getResult($attemptNumber + 1, $request); + } + return $result; + } + throw new DirectApiException('Не удалось получить отчёт после 100 попыток'); + } + private function parseApiError($errorString) { - $errorJson= json_decode($errorString); + $errorJson = json_decode($errorString); $requestId = (string)$errorJson->request_id; $errorCode = (int)$errorJson->error_code; $errorMessage = (string)$errorJson->error_string;