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 = ['accesskey' => $this->apiKey, 'uastrig' => $userAgent];
$body = http_build_query($params, null, '&');
$request = new Request('POST', self::$uri, ['Content-Type' => 'application/x-www-form-urlencoded'], $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 found?
*/
if (isset($content->flag) && $content->flag == 3) {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
/*
* Errors
*/
if (isset($content->flag) && $content->flag == 4) {
throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName());
}
if (isset($content->flag) && $content->flag == 6) {
throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName());
}
if (isset($content->flag) && $content->flag > 3) {
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->info)) {
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
}
return $content;
}