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_key' => $this->apiKey, 'user_agent' => $userAgent];
$body = http_build_query($params, null, '&');
$request = new Request('POST', self::$uri, [], $body);
$response = $this->getResponse($request);
/*
* no json returned?
*/
$contentType = $response->getHeader('Content-Type');
if (!isset($contentType[0]) || $contentType[0] != 'application/json') {
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());
/*
* No result
*/
if (isset($content->message_code) && $content->message_code == 'no_user_agent') {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
/*
* Limit exceeded
*/
if (isset($content->message_code) && $content->message_code == 'usage_limit_exceeded') {
throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName());
}
/*
* Error
*/
if (isset($content->message_code) && $content->message_code == 'no_api_user_key') {
throw new Exception\InvalidCredentialsException('Missing API key for ' . $this->getName());
}
if (isset($content->message_code) && $content->message_code == 'user_key_invalid') {
throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName());
}
if (!isset($content->result) || $content->result !== 'success') {
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
}
/*
* Missing data?
*/
if (!$content instanceof stdClass || !isset($content->parse) || !$content->parse instanceof stdClass) {
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
}
return $content->parse;
}