Helper functions to retrieve external XML feed, cache and render into templates.
- silverstripe/cms ^4.3.x
- silverstripe/framework ^4.3.x
composer require toastnz/xmlfeed
Configure the default Cache Lifetime or default feed URL:
Toast\XMLFeed\Feed:
CacheLifetime: 3600
URL: "https://external.feed.url/feed.xml"
use Toast\XMLFeed;
$feed = Feed::get($url = null, $xmlPath = null, $cacheLifetime = 300, $asArray = false, $flushCache = false);
By default an ArrayList will be returned, which can be rendered directly into a template.
Note: you must check the raw contents of the XML feed to identify fields names.
XML:
<RSS>
<Items>
<Item>
<Headline>Sed in viverra dui. Nullam vel congue massa.</Headline>
<Summary>Ut id nisi vitae massa consectetur dictum quis sed sapien. At euismod turpis</Summary>
</Item>
<Item>
<Headline>Aliquam dictum finibus magna</Headline>
<Summary>Cras mattis non elit sit amet vulputate. Nunc at metus sed sapien eros.</Summary>
</Item>
</Items>
</RSS>
PHP:
use Toast\XMLFeed;
...
class PageController extends ContentController
{
public function getNewsFeed()
{
return Feed::get('https://newswebsite.url/news-feed.xml', 'RSS.Items');
}
}
Template:
<% loop $NewsFeed %>
<h1>$Headline.XML</h1>
<p>$Summary.XML</p>
<% end_loop %>