Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

How to parse feed from existing string #413

Description

@dvikan

I have an existing feed in a string and I wanted to parse it. So I don't need feed-io to fetch it via network. So I am answering my own question with an example:

<?php

namespace kek;

use FeedIo\Adapter\NullClient;
use FeedIo\Feed;
use FeedIo\Parser\XmlParser;
use FeedIo\Reader;
use FeedIo\Reader\Document;
use FeedIo\Rule\DateTimeBuilder;
use FeedIo\Standard\Rss;
use Psr\Log\NullLogger;

require 'vendor/autoload.php';

$client = new NullClient();
$logger = new NullLogger();
$reader = new Reader($client, $logger);
$reader->addParser(new XmlParser(new Rss(new DateTimeBuilder()), $logger));
$document = new Document(file_get_contents('https://news.ycombinator.com/rss'));

$feed = $reader->parseDocument($document, new Feed());

echo "feed title : {$feed->getTitle()} \n";

foreach ($feed as $item) {
    print "  {$item->getTitle()}\n";
}

As far as I understand this is the way to go when wanting to parse a feed when you already have it as a string. Has a readString() method in FeedIo been considered? Would be nice to have all the parser already loaded etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions