Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Pulling RSS feeds from other sites onto my home page.


Reply

31 Posts   4975 Views

Avatar
Mo

1 November 2009 at 8:53am Community Member, 509 Posts

I have literally done just this for a clients site. What I did was add the ability to add up to 3 feeds into a field in the CMS, like this:

public static $db = array(
   'Feed1Name'   => 'Text',
   'Feed1URL'   => 'Text',
   'Feed2Name'   => 'Text',
   'Feed2URL'   => 'Text',
   'Feed3Name'   => 'Text',
   'Feed3URL'   => 'Text'
);

Then amended the Method to allow you to load in theses feeds from your template, this is a bit tricky because silverstripes template language doesn't allow you to load variables into methods, so instead I created an array to store the feeds within the method, and instead access them via a number.

So something like this:

public function getItemsFromRSS($feedNum = 0) {      
   include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/SimplePie.php'));

   $feedItem = array();
   array_push($feedItem,$feedsPage->Feed1URL);
   array_push($feedItem,$feedsPage->Feed2URL);
   array_push($feedItem,$feedsPage->Feed3URL);

   $output = new DataObjectSet();

   $feed = new SimplePie($feedItem[$feedNum], TEMP_FOLDER);
   $feed->init();
   if($items = $feed->get_items(0, $feedItems)) {
      foreach($items as $item) {

         // Cast the Title
         $title = new Text('Title');
         $title->setValue($item->get_title());

         $output->push(new ArrayData(array(
            'Title'         => $title,
            'Link'         => $item->get_link()
         )));
      }
      return $output;
   }
}

Finally, in your template, call this using something like:

<div class="NewsFeed">
   <h2>
      $Feed1Name
   </h2>
   <ul>
      <% control getItemsFromRSS(1) %>
         <li>$Title <a href="$Link" target="_blank">read more</a></li>
      <% end_control %>
   </ul>
</div>

Its not the best approach, if you add more than 3 feeds to the CMS, you need to add additional array_push($feedItem,$feedsPage->Feed3URL); to your method to account.

An alternative would be to use a dataobject, but I havent really had time for that :)

Mo

Avatar
zim

2 November 2009 at 5:14am Community Member, 135 Posts

Hi Mo,

I have put the code in and now have the six fields in my cms where i add name of feed and link to feed eg http://feeds.guardian.co.uk/theguardian/world/rss

however I am getting this error when i try to view the page

[Notice] Undefined variable: feedsPage
GET /SilverStripe01/?flush=1

Line 114 in C:\wamp\www\SilverStripe01\mysite\code\HomePage.php

113 $feedItem = array();
114 array_push($feedItem,$feedsPage->Feed1URL);
115 array_push($feedItem,$feedsPage->Feed2URL);
116 array_push($feedItem,$feedsPage->Feed3URL);

Do you know what i might be doing wrong.

thanks for help

Avatar
Mo

3 November 2009 at 3:10am Community Member, 509 Posts

Oops, sorry, I my fault, I was doing something a little bit different with my method and left some incorrect code in.

Each array_push() should look something like:

array_push($feedItem,$this->FeedXXURL);

Mo

Avatar
zim

3 November 2009 at 4:47am Community Member, 135 Posts

Mo,

it is only getting first Feed1Name, then nothing. any idea?

this is what i have

public function getItemsFromRSS($feedNum = 0) {
include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/SimplePie.php'));

$feedItem = array();
array_push($feedItem,$this->Feed1URL);
array_push($feedItem,$this->Feed2URL);
array_push($feedItem,$this->Feed3URL);

$output = new DataObjectSet();

$feed = new SimplePie($feedItem[$feedNum], TEMP_FOLDER);
$feed->init();
if($items = $feed->get_items(0, $feedItems)) {
foreach($items as $item) {

// Cast the Title
$title = new Text('Title');
$title->setValue($item->get_title());

$output->push(new ArrayData(array(
'Title' => $title,
'Link' => $item->get_link()
)));
}
return $output;
}
}

and

<div class="NewsFeed">
       <h2>
          $Feed1Name
       </h2>
       <ul>
          <% control getItemsFromRSS(1) %>
             <li>$Title <a href="$Link" target="_blank">read more</a></li>
          <% end_control %>
       </ul>
      </div>

Avatar
zim

3 November 2009 at 4:57am Community Member, 135 Posts

Hi Mo,

I have figured it out. It now works.

I had to change this

if($items = $feed->get_items(0, $feedItems)) {
to

if($items = $feed->get_items(0, $feedItem)) {

eg $feedItem not $feedItems

now works.

Thanks for your help dude!

Avatar
Mo

3 November 2009 at 7:13am Community Member, 509 Posts

No Probs,

Actually, looking at that, you should replace "$feedItems" with 1. That line is what specifies how many articles to pull down from the feed, so should be an integer. Not really sure why it is working for you now, but that seems to me that it might become a bit unstable.

Mo

Avatar
zim

4 November 2009 at 3:25am Community Member, 135 Posts

Hey mo,

all good, except that one of the feed titles has a & in it and is causing error

XML Parsing Error: not well-formed
Location: http://localhost/SilverStripe01/
Line Number 236, Column 150:

is there an easy way to replace & with &amp;

I will try and find out but if you know easy way that would be great :)

Avatar
Samba Sam

6 November 2009 at 2:29am (Last edited: 6 November 2009 2:34am), Community Member, 85 Posts

Howdy folks,
Very useful info here. I am able to pull multiple feeds onto a SS webpage - great!
What I would like to do is have the link under each post title "<li>$Title <a href="$Link" target="_blank">read more</a></li>" link to and display the full post/description in a new webpage WITHIN my template, rather than linking to new website.

Also, I can't get the dates of the posts to show. I've add $date to the suggested code as in:

...
if($items = $feed->get_items(0, $this->NumberToShow)) {
foreach($items as $item) {
// Cast the Date
$date = new Date('Date');
$date->setValue($item->get_date());
// Cast the Title
$title = new Text('Title');
$title->setValue($item->get_title());
$output->push(new ArrayData(array(
'Title' => $title,
'Date' => $date,
'Link' => $item->get_link()
)));
}
return $output;
}
}

Adding the $date doesn't appear to change anything.

Thanks for the help,
Sam