Jump to:

23370 Posts in 18160 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » Pulling RSS feeds from other sites onto my home page.

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

Page: 1 2 3 4
Go to End
Author Topic: 4810 Views
  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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>

  • zim
    Avatar
    Community Member
    135 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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!

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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

  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Re: Pulling RSS feeds from other sites onto my home page. Link to this post

    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

    4810 Views
Page: 1 2 3 4
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.