You are here

protected function HttpFetcher::get in Feeds 8.3

Performs a GET request.

Parameters

string $url: The URL to GET.

string $sink: The location where the downloaded content will be saved. This can be a resource, path or a StreamInterface object.

string $cache_key: (optional) The cache key to find cached headers. Defaults to false.

Return value

\Guzzle\Http\Message\Response A Guzzle response.

Throws

\RuntimeException Thrown if the GET request failed.

See also

\GuzzleHttp\RequestOptions

1 call to HttpFetcher::get()
HttpFetcher::fetch in src/Feeds/Fetcher/HttpFetcher.php
Fetch content from a feed and return it.

File

src/Feeds/Fetcher/HttpFetcher.php, line 139

Class

HttpFetcher
Defines an HTTP fetcher.

Namespace

Drupal\feeds\Feeds\Fetcher

Code

protected function get($url, $sink, $cache_key = FALSE) {
  $url = Feed::translateSchemes($url);
  $options = [
    RequestOptions::SINK => $sink,
  ];

  // Adding User-Agent header from the default guzzle client config for feeds
  // that require that.
  if (isset($this->client
    ->getConfig('headers')['User-Agent'])) {
    $options[RequestOptions::HEADERS]['User-Agent'] = $this->client
      ->getConfig('headers')['User-Agent'];
  }

  // Add cached headers if requested.
  if ($cache_key && ($cache = $this->cache
    ->get($cache_key))) {
    if (isset($cache->data['etag'])) {
      $options[RequestOptions::HEADERS]['If-None-Match'] = $cache->data['etag'];
    }
    if (isset($cache->data['last-modified'])) {
      $options[RequestOptions::HEADERS]['If-Modified-Since'] = $cache->data['last-modified'];
    }
  }
  try {
    $response = $this->client
      ->get($url, $options);
  } catch (RequestException $e) {
    $args = [
      '%site' => $url,
      '%error' => $e
        ->getMessage(),
    ];
    throw new \RuntimeException($this
      ->t('The feed from %site seems to be broken because of error "%error".', $args));
  }
  if ($cache_key) {
    $this->cache
      ->set($cache_key, array_change_key_case($response
      ->getHeaders()));
  }
  return $response;
}