protected function getResult($userAgent, array $headers)
{
/*
* an empty UserAgent makes no sense
*/
if ($userAgent == '') {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
$params = ['user-id' => $this->apiUserId, 'api-key' => $this->apiKey, 'output-format' => 'json', 'output-case' => 'snake', 'user-agent' => $userAgent];
$body = http_build_query($params, null, '&');
$request = new Request('POST', self::$uri, ['Content-Type' => 'application/x-www-form-urlencoded'], $body);
try {
$response = $this->getResponse($request);
} catch (Exception\RequestException $ex) {
/* @var $prevEx \GuzzleHttp\Exception\ClientException */
$prevEx = $ex->getPrevious();
if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 403) {
throw new Exception\InvalidCredentialsException('Your API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex);
}
throw $ex;
}
/*
* no json returned?
*/
$contentType = $response->getHeader('Content-Type');
if (!isset($contentType[0]) || $contentType[0] != 'application/json;charset=UTF-8') {
throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
}
$content = json_decode($response->getBody()->getContents());
/*
* errors
*/
if (isset($content->api_error)) {
switch ($content->api_error) {
case 1:
throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
break;
case 2:
throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" for ' . $this->getName());
break;
default:
throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
break;
}
}
/*
* Missing data?
*/
if (!$content instanceof stdClass) {
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
}
return $content;
}