Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse feed from existing string #413

Open
dvikan opened this issue Dec 15, 2022 · 0 comments
Open

How to parse feed from existing string #413

dvikan opened this issue Dec 15, 2022 · 0 comments

Comments

@dvikan
Copy link
Contributor

dvikan commented Dec 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant