You are here

public static function Reader::import in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-feed/src/Reader/Reader.php \Zend\Feed\Reader\Reader::import()

Import a feed by providing a URI

Parameters

string $uri The URI to the feed:

string $etag OPTIONAL Last received ETag for this resource:

string $lastModified OPTIONAL Last-Modified value for this resource:

Return value

Feed\FeedInterface

Throws

Exception\RuntimeException

Overrides ReaderImportInterface::import

1 call to Reader::import()
FeedSet::offsetGet in vendor/zendframework/zend-feed/src/Reader/FeedSet.php
Supports lazy loading of feeds using Reader::import() but delegates any other operations to the parent class.

File

vendor/zendframework/zend-feed/src/Reader/Reader.php, line 190

Class

Reader

Namespace

Zend\Feed\Reader

Code

public static function import($uri, $etag = null, $lastModified = null) {
  $cache = self::getCache();
  $client = self::getHttpClient();
  $client
    ->resetParameters();
  $headers = new ZendHttp\Headers();
  $client
    ->setHeaders($headers);
  $client
    ->setUri($uri);
  $cacheId = 'Zend_Feed_Reader_' . md5($uri);
  if (static::$httpConditionalGet && $cache) {
    $data = $cache
      ->getItem($cacheId);
    if ($data) {
      if ($etag === null) {
        $etag = $cache
          ->getItem($cacheId . '_etag');
      }
      if ($lastModified === null) {
        $lastModified = $cache
          ->getItem($cacheId . '_lastmodified');
      }
      if ($etag) {
        $headers
          ->addHeaderLine('If-None-Match', $etag);
      }
      if ($lastModified) {
        $headers
          ->addHeaderLine('If-Modified-Since', $lastModified);
      }
    }
    $response = $client
      ->send();
    if ($response
      ->getStatusCode() !== 200 && $response
      ->getStatusCode() !== 304) {
      throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response
        ->getStatusCode());
    }
    if ($response
      ->getStatusCode() == 304) {
      $responseXml = $data;
    }
    else {
      $responseXml = $response
        ->getBody();
      $cache
        ->setItem($cacheId, $responseXml);
      if ($response
        ->getHeaders()
        ->get('ETag')) {
        $cache
          ->setItem($cacheId . '_etag', $response
          ->getHeaders()
          ->get('ETag')
          ->getFieldValue());
      }
      if ($response
        ->getHeaders()
        ->get('Last-Modified')) {
        $cache
          ->setItem($cacheId . '_lastmodified', $response
          ->getHeaders()
          ->get('Last-Modified')
          ->getFieldValue());
      }
    }
    return static::importString($responseXml);
  }
  elseif ($cache) {
    $data = $cache
      ->getItem($cacheId);
    if ($data) {
      return static::importString($data);
    }
    $response = $client
      ->send();
    if ((int) $response
      ->getStatusCode() !== 200) {
      throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response
        ->getStatusCode());
    }
    $responseXml = $response
      ->getBody();
    $cache
      ->setItem($cacheId, $responseXml);
    return static::importString($responseXml);
  }
  else {
    $response = $client
      ->send();
    if ((int) $response
      ->getStatusCode() !== 200) {
      throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response
        ->getStatusCode());
    }
    $reader = static::importString($response
      ->getBody());
    $reader
      ->setOriginalSourceUri($uri);
    return $reader;
  }
}