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.


Go to End
Reply


31 Posts   5114 Views

Avatar
Samba Sam

Community Member, 85 Posts

6 November 2009 at 6:43am

Edited: 06/11/2009 6:49am

Hi,
So I was able to get the date to display by the following code. I removed the "cast the date/title stuff" and added '$item->get_date' to '$output'. By the way, what was that cast stuff for? It didn't seem to make any difference.
....
foreach($items as $item) {
$output->push(new ArrayData(array(
'Title' => $item->get_title(),
'Date' => $item->get_date('Y-m-d'),
'Link' => $item->get_link()
)));
....

My RSSPage.ss looks like:
<% control getItemsFromRSS(0) %>
       <li>$Title<br>$Date<a href="$Link" target="_blank">read more</a></li>
<% end_control %>
...

I still would like to know how to link and display the full post within my website.

Any ideas anyone?

Sam

Avatar
Mo

Community Member, 510 Posts

6 November 2009 at 12:47pm

I believe that you cast the variable, so that you minimise the chance of sending the wrong type of variable to your script and causing it to fall over.

I also believe (but may be mistaken) that casting takes out some of the potential nasties you might get from loading unchecked data (IE injection attacks etc).

In answer to your question about having the story appear in your page. You would have to amend your method (or write a new one) that would also pull in the RSS feeds description. I doubt this will be the whole article though, RSS feeds usually only hold a summary of the story.

I am not sure what you would need to do to load this., I cant imagein ti would be that difficult though. I believe there is a get_decription() method. I would check Simplepie's documentation (http://simplepie.org/wiki/) and there API reference (http://simplepie.org/wiki/reference/start).

Mo

Avatar
zim

Community Member, 135 Posts

6 November 2009 at 1:06pm

Respect to the Mo!! x

Avatar
Samba Sam

Community Member, 85 Posts

7 November 2009 at 10:47am

Edited: 07/11/2009 10:50am

Yes respect to Mo.

I am able to get the description to show on the same page as the title, etc, which is great, by adding the get_description to the "foreach($items as $item)" statement to my RSSPage.php. For RSS feeds with longer descriptions (e.g., PUBMED), I would like to be able to link to a separate page on my website to display the full content. But, that seems like a fairly complicated endeavor, beyond my current ability. Also, I kept the Title and Content "casted", but not the date as I couldn't figure out how to change the date format when it was "casted".

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);

//init the process
$feed->init();

if($items = $feed->get_items(0, $this->NumberToShow)) {
   
foreach($items as $item) {

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

// Cast the Content
$content = new Text('Content');
$content->setValue($item->get_description());

$output->push(new ArrayData(array(
'Title' => $title,
'Content' => $content,
'Date' => $item->get_date ('j F Y, g:i a'),
'Link' => $item->get_link()
)));
}
return $output;
}
}

I also added a "NumberTOShow" variable to the CMS:
...
function getCMSFields() {
   $fields = parent::getCMSFields();
   $fields->addFieldToTab("Root.Content.RSS", new NumericField("NumberToShow", "Number of Items to show"));
...

Sam

Avatar
Mo

Community Member, 510 Posts

10 November 2009 at 9:10am

Happy to help :). Not sure what's happening with your date option. Maybe RSS feeds don't format the date in a correct format? You may need to write a function that de constructs the date and then rebuilds it using the php date() function?

As for loading the whole story, It would be a big pain in the ass. The only way I can think to do it would be to screen scrape the target site for its content, which is not easy on complex sites. Also, I would also advise caution, because if you are taking the whole story, the original article's author might not be happy with you presenting it in a manner that might make it appear like your work. You might end up with a nasty law suit (I have seen it happen before, its not nice). So I would suggest sticking with just the description, unless you have explicit permission of the content author/editor.

Hope thats some help :)

Mo

Avatar
davidm2010

Community Member, 107 Posts

20 January 2010 at 12:09am

This has been a great posting. I worked on this problem for several weeks. I started with Flash, it allows for xml parsing, but won't allow to pull data from a remote server. I found this and it was perfect. By the way, the google apps works pretty well, but you get very little control.

By the way, for the below listed code:

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

So where does this get placed?

Also, does simpliepie parse the xml or is that the PHP parsing function? I have it turned on, but was curious.

Thanks to all who worked on this.
(p.s. don't use "view format help" It covers the post button and you can't post)

David

Avatar
SSadmin

Community Member, 90 Posts

9 March 2010 at 2:41pm

It has been a great Tutrial for Pulling outside RSS feeds into SS website. Heaps of Informaiton was quite essential for me as a newbie to handle the RSS stuff.
I was using the RESTFULSERVICE First, seems the tutorial feeds works but mine seems hard to work right.
Then Basically, i was refering the Blog Moduel RSS widget stuff for structuring me coding function in Page.php using SimplePie RSS parser.
In the second way, was quite good though. Simple and Easy.

Just donnot know why the restfulservice doesnt work well on mine XML. Wonderring maybe the XML nodes' different?!

My RSS feed url:http://rss.nzherald.co.nz/rss/xml/nzhrsscid_001500879.xml

The errors:
[Warning] SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found.

Go to Top