JSONFeed is a pragmatic syndication format, like RSS and Atom, but with one big difference: it’s JSON instead of XML.
This library provides functionnalits for mananaging feed through your PHP code. It provides a natural syntax for accessing elements of feed.
Install the library using composer:
{
"require": {
"jdecool/jsonfeed": "^0.1"
}
}
Create your feed :
use DateTime;
use DateTimeZone;
use JDecool\JsonFeed\Author;
use JDecool\JsonFeed\Feed;
use JDecool\JsonFeed\Item;
$author = new Author('Brent Simmons');
$author->setUrl('http://example.org/');
$author->setAvatar('https://example.org/avatar.png');
$item = new Item('2347259');
$item->setUrl('https://example.org/2347259');
$item->setDatePublished(new DateTime('2016-02-09 14:22:00', new DateTimeZone('+0200')));
$item->setContentText('Cats are neat. https://example.org/cats');
$feed = new Feed('Brent Simmons’s Microblog');
$feed->setUserComment('This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json');
$feed->setHomepageUrl('https://example.org/');
$feed->setFeedUrl('https://example.org/feed.json');
$feed->setAuthor($author);
$feed->addItem($item);
Render it :
use JDecool\JsonFeed\Writer\RendererFactory;
$renderer = $factory->createRenderer(RendererFactory::VERSION_1);
header('Content-Type: application/json');
echo $renderer->render($feed);