Documentation

In this page, you will learn how to use feed-io and useful tips to extend it. If you need to go further, please refer to the API Reference

Getting a feed-io instance

FeedIo is the main class of the library. It acts as a facade designed to help you dealing with feeds. It has two dependencies :

Using the factory

It is the easiest way to load FeedIo

                <?php
require 'vendor/autoload.php';

use FeedIo\Factory;

$feedIo = Factory::create()->getFeedIo();
        

Using rss-atom-bundle

If your application is built with Symfony, you can use rss-atom-bundle to invoke Feed-Io using the service container. In your controller it looks like this :

                    $feedIo = $this->container->get('feedio');
            

In a Slim Application

If your application is built with Slim, you can add feed-io in your dependencies and inject Slim's Monolog instance into it. Just add this in dependencies.php

                    
$container['feedio'] = function ($c) {
    $client = new \FeedIo\Adapter\Guzzle\Client(new GuzzleHttp\Client());
    return new \FeedIo\FeedIo($client, $c->logger);
};
                    
            

Do it yourself

Convenient if you need to inject your own dependencies. In the example below, we inject Monolog configured to write logs on the standard output.

                    <?php
require 'vendor/autoload.php';

use FeedIo\FeedIo;
use FeedIo\Adapter\Guzzle\Client;
use GuzzleHttp\Client as GuzzleClient;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$client = new Client(new GuzzleClient());
$logger = new Logger('default', [new StreamHandler('php://stdout')]);

$feedIo = new FeedIo($client, $logger);

        
    

Writing feeds

Building the Feed

Before writing the feed in a specific format, you need to build an instance of FeedInterface. feed-io comes with the Feed class that implements this interface, but you can use another class if you need to as long as it implements FeedInterface. Also, the items you add in it must implement ItemInterface.

FeedInterface and ItemInterface are essentials in feed-io. If you want to extend the library with your own classes, you must implement those two interfaces.

                <?php
require 'vendor/autoload.php';

use \FeedIo\Factory;
use \FeedIo\Feed;

$feed = new Feed();
$feed->setLink('https://feed-io.net');
$feed->setTitle('feed-io example feed');

// The item instance SHOULD be instanciated by the feed
$item = $feed->newItem();

$item->setTitle('a title');
$item->setLastModified(new \DateTime());
$item->setLink('https://feed-io.net/item/1');
$item->setDescription("Hope you like the code you are reading");

$feed->add($item);

$feedIo = Factory::create()->getFeedIo();
echo $feedIo->format($feed, 'atom');

    

Reading feeds

Principle

When feedIo reads a feed, it returns an instance of Result. This object contains several attributes related to the request :

  • The raw response as fetched by the HTTP client (Guzzle)
  • The original JSON or XML stream embedded in a Document
  • The date and time of the call
  • The 'modifiedSince' parameter used to filter items
  • The feed, embedded in a Feed

Basically, a simple read access works as follow :


$result = $feedIo->read($url);
$feed = $result->getFeed();
        

The Feed implements the Iterator interface, which allows you to walk through items in a foreach:


echo "feed title : {$feed->getTitle()} \n";
foreach ($feed as $item) {
    echo "{$item->getLastModified()->format(\DateTime::ATOM)} : {$item->getTitle()} \n";
    echo "{$item->getDescription()} \n";
}

Fetching only the newest feeds

Let's assume you stored the 'modifiedSince' value somewhere and you have it in the $modifiedSince variable :


$result = $feedIo->readSince($url, $modifiedSince);
            

The result will only contain items published after $modifiedSince