Back to FeedFactory class

Method getFeed

public \Joomla\CMS\Feed\Feed
getFeed
(mixed $uri)
Method to load a URI into the feed reader for parsing.
Parameters
  • string $uri The URI of the feed to load. Idn uris must be passed already converted to punycode.
Returns
  • \Joomla\CMS\Feed\Feed
Since
  • 3.1.4
-
  • \InvalidArgumentException
  • \RuntimeException
Class: FeedFactory
Project: Joomla

Method getFeed - Source code

/**
 * Method to load a URI into the feed reader for parsing.
 *
 * @param   string  $uri  The URI of the feed to load. Idn uris must be passed already converted to punycode.
 *
 * @return  Feed
 *
 * @since   3.1.4
 * @throws  \InvalidArgumentException
 * @throws  \RuntimeException
 */
public function getFeed($uri)
{
    // Create the XMLReader object.
    $reader = new \XMLReader();
    // Open the URI within the stream reader.
    if (!@$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
        // Retry with JHttpFactory that allow using CURL and Sockets as alternative method when available
        // Adding a valid user agent string, otherwise some feed-servers returning an error
        $options = new Registry();
        $options->set('userAgent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0');
        try {
            $response = HttpFactory::getHttp($options)->get($uri);
        } catch (\RuntimeException $e) {
            throw new \RuntimeException('Unable to open the feed.', $e->getCode(), $e);
        }
        if ($response->code != 200) {
            throw new \RuntimeException('Unable to open the feed.');
        }
        // Set the value to the XMLReader parser
        if (!$reader->XML($response->body, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
            throw new \RuntimeException('Unable to parse the feed.');
        }
    }
    try {
        // Skip ahead to the root node.
        while ($reader->read()) {
            if ($reader->nodeType == \XMLReader::ELEMENT) {
                break;
            }
        }
    } catch (\Exception $e) {
        throw new \RuntimeException('Error reading feed.', $e->getCode(), $e);
    }
    // Setup the appropriate feed parser for the feed.
    $parser = $this->_fetchFeedParser($reader->name, $reader);
    return $parser->parse();
}