/**
  * {@inheritdoc}
  */
 public function fetch(FeedInterface $feed)
 {
     $request = $this->httpClient->createRequest('GET', $feed->getUrl());
     $feed->source_string = FALSE;
     // Generate conditional GET headers.
     if ($feed->getEtag()) {
         $request->addHeader('If-None-Match', $feed->getEtag());
     }
     if ($feed->getLastModified()) {
         $request->addHeader('If-Modified-Since', gmdate(DateTimePlus::RFC7231, $feed->getLastModified()));
     }
     try {
         $response = $this->httpClient->send($request);
         // In case of a 304 Not Modified, there is no new content, so return
         // FALSE.
         if ($response->getStatusCode() == 304) {
             return FALSE;
         }
         $feed->source_string = $response->getBody(TRUE);
         $feed->setEtag($response->getHeader('ETag'));
         $feed->setLastModified(strtotime($response->getHeader('Last-Modified')));
         $feed->http_headers = $response->getHeaders();
         // Update the feed URL in case of a 301 redirect.
         if ($response->getEffectiveUrl() != $feed->getUrl()) {
             $feed->setUrl($response->getEffectiveUrl());
         }
         return TRUE;
     } catch (RequestException $e) {
         $this->logger->warning('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage()));
         drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'warning');
         return FALSE;
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function fetch(FeedInterface $feed)
 {
     $request = new Request('GET', $feed->getUrl());
     $feed->source_string = FALSE;
     // Generate conditional GET headers.
     if ($feed->getEtag()) {
         $request = $request->withAddedHeader('If-None-Match', $feed->getEtag());
     }
     if ($feed->getLastModified()) {
         $request = $request->withAddedHeader('If-Modified-Since', gmdate(DateTimePlus::RFC7231, $feed->getLastModified()));
     }
     try {
         /** @var \Psr\Http\Message\UriInterface $actual_uri */
         $actual_uri = NULL;
         $response = $this->httpClientFactory->fromOptions(['allow_redirects' => ['on_redirect' => function (RequestInterface $request, ResponseInterface $response, UriInterface $uri) use(&$actual_uri) {
             $actual_uri = (string) $uri;
         }]])->send($request);
         // In case of a 304 Not Modified, there is no new content, so return
         // FALSE.
         if ($response->getStatusCode() == 304) {
             return FALSE;
         }
         $feed->source_string = (string) $response->getBody();
         if ($response->hasHeader('ETag')) {
             $feed->setEtag($response->getHeaderLine('ETag'));
         }
         if ($response->hasHeader('Last-Modified')) {
             $feed->setLastModified(strtotime($response->getHeaderLine('Last-Modified')));
         }
         $feed->http_headers = $response->getHeaders();
         // Update the feed URL in case of a 301 redirect.
         if ($actual_uri && $actual_uri !== $feed->getUrl()) {
             $feed->setUrl($actual_uri);
         }
         return TRUE;
     } catch (RequestException $e) {
         $this->logger->warning('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage()));
         drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'warning');
         return FALSE;
     }
 }