/** * {@inheritdoc} * * @param string $source Source. * @param EncapsulatedOptions|null $options Optional. Options. * * @return string Response. * * @throws \InvalidArgumentException Options is not an instance of HttpOptions. * @throws HttpConnectionException Failed to connect to source. * @throws HttpServerException Server sent an error code. */ public function fetchFreshData($source, EncapsulatedOptions $options = null) { if ($options && !$options instanceof HttpOptions) { throw new \InvalidArgumentException('Options must be an instance of HttpOptions.'); } return \ScriptFUSION\Retry\retry($this->getTries(), function () use($source, $options) { if (false === ($response = @file_get_contents($this->getOrCreateUrlBuilder()->buildUrl($source, $options ? $options->getQueryParameters() : [], $this->getBaseUrl()), false, stream_context_create(['http' => ['ignore_errors' => true] + array_merge($this->options->extractHttpContextOptions(), $options ? $options->extractHttpContextOptions() : [])])))) { $error = error_get_last(); throw new HttpConnectionException($error['message'], $error['type']); } $code = explode(' ', $http_response_header[0], 3)[1]; if ($code < 200 || $code >= 400) { throw new HttpServerException("HTTP server responded with error: \"{$http_response_header['0']}\".\n\n{$response}"); } return $response; }, new ExponentialBackoffErrorHandler()); }
public function testOptionDefaults() { $options = new HttpOptions(); self::assertSame([], $options->getQueryParameters()); self::assertSame([], $options->getHeaders()); }