Solved

Loading Multiple RSS feeds into specific Divs

Posted on 2010-08-18
3
466 Views
Last Modified: 2013-11-19
I am currently working on a page that has several divs, each of which needs to display an imagelink to a specified RSS feed's latest story.

I've managed to get RSS feeds to load into the page using Google's API, however I was unable to get it to load more than one at a time.

Can someone point me to a tutorial on getting just the basic info from a Feed(Title, URL), as all the java and php tutorials i've seen are pulling large amounts of data I won't use and also only handling one feed.
0
Comment
Question by:OrbusEddy
3 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33472598
I wrote an EE article a while back which was to do with storing data in memory, but it used an RSS feed as the source for its data. Maybe have a look at that and see if it points you in the right direction, then come back and ask more specific questions that the article does not cover.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3212-Using-PHP-classes-to-group-data-in-memory.html

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33474518
RSS is a specialized subset of XML.  Good description here:
http://cyber.law.harvard.edu/rss/rss.html

Since it is XML, you can access the information in the RSS feed with SimpleXML; example in the code snippet.  If you have several RSS feeds, you would just read them one at a time and extract the elements of the object you want to keep.  Then produce the <div> statements.  Easy!

best regards, ~Ray
<?php // RAY_rss_parser.php
error_reporting(E_ALL);

// GRAB AN RSS FEED AND TURN IT INTO A BOX ON A WEB PAGE
// USEFUL MAN PAGE: http://us2.php.net/manual/en/book.simplexml.php

// CHOOSE YOUR FEED HERE
$rss_feed_url = 'http://www.nationalpres.org/rss/sermons.xml';

// ACQUIRE THE RSS FEED IN A DATA STRING
if (!$rss_xml_string = @file_get_contents($rss_feed_url))
{
    echo "<p>No RSS at $rss_feed_url</p>\n";
    die("Quel Fromage");
}

// TURN THE XML FEED INTO AN OBJECT
$rss_object = SimpleXML_Load_String($rss_xml_string);

// ACTIVATE THIS TO LOOK AT THE OBJECT
// echo "<pre>"; var_dump($rss_object); die('Foo');

// URL OF THE RSS FEED 'DISPLAYED' IN HTML COMMENT
echo "\n<!-- $rss_feed_url -->\n\n";

// START DIV
echo "<div id=\"box\">\n";

// GET THE CHANNEL INFORMATION
$channel_title = $rss_object->channel->title;
$channel_link  = $rss_object->channel->link;
$channel_title = htmlentities($channel_title);
echo "<h3><a href=\"$channel_link\">$channel_title</a></h3>\n";

// ITERATE OVER THE ITEMS IN THE CHANNEL
foreach ($rss_object->channel->item as $item)
{
    $item_title = $item->title;
    $item_link  = $item->link;
    $item_title = htmlentities($item_title);
    echo "<p><a href=\"$item_link\">$item_title</a></p>\n";
}

// WRAPUP DIV
echo "</div id=\"box\">\n";

// DEBUGGING CODE - SHOW THE ENTIRE OBJECT
echo "<pre>";
var_dump($rss_object);

Open in new window

0
 

Expert Comment

by:cptnem0
ID: 34549621
Great stuff Ray! Thanks.

How would I limit the amount of items in the feed? Say I wanted on 3 of the most recent items on the feed?
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now