6CC001 Workshop - week 06

From mi-linux
Revision as of 14:51, 29 March 2011 by 0811816 (talk | contribs)
Jump to navigationJump to search

Main Page >> Advanced Web Technologies >> Workbook >> Week 06

Today let’s play with RSS feeds. There are 2 things you can do:

  • Consume an existing third-party feed
  • Create your own feed, for other users to consume

Consume an existing third-party feed

Since PHP 5, consuming an XML file (remember that’s all an RSS feed is!) couldn’t be more simple:

  $feed_url = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml";

  // Get data from feed file
  $response = file_get_contents($feed_url);
    
  // Insert XML into structure
  $xml = simplexml_load_string($response); 

  // Browse structure
  foreach($xml->channel->item as $one_item)
    echo $one_item->title."<BR>"; 

First you load the content of the remote file into a variable, using the file_get_contents function.

Then you concert this big blob of data into a more friendly, loop-able structured object, using the simplexml_load_string function.

And finally, you loop through your object and display the data on the screen. This example only displays the title of the different articles. Note: You might want to improve this part (items in a list, clickable links etc…)

Create your own feed, for other users to consume

Again, no rocket science here. The code below does this:

  1. It connects to a database and retrieves the 10 latest articles (or in this case messages from our blog, see earlier week)
  2. It then loops through them, and concatenate the data with the relevant XML tags (see RSS standard).
  3. Finally it displays it straight onto the screen. You could also save your XML variable to a text file, to create a cached version of your feed (which would be more efficient as there is no need to regenerate your feed every time if your data doesn’t change!).
// Connect to database

// Get latest items from db
$sql = "SELECT title, link, message FROM messages ORDER BY date_added DESC limit 0,10";
$rst = mysql_query($sql);

// Loop through data and build feed items
$items = "";
while($a_row = mysql_fetch_assoc($rst))
{
$items.= "<item>";
$items.= " <title>{$a_row['title']}</title>";
$items.= " <link>{$a_row['link']}</link>";
$items.= " <description>{$a_row['message']}</description>";
$items.= "</item>"; 
}

// Build feed text
$feed = "<?xml version=\"1.0\"?>";
$feed.= "<rss version=\"2.0\">";
$feed.= " <channel>";
$feed.= " <title>My cool feed</title>";
$feed.= " <link>http://www.dude.com/</link>";
$feed.= " <description>A cool feed</description>";
$feed.= " $items";
$feed.= " </channel>";
$feed.= "</rss>";

// Display feed text
echo $feed;

All done!

Now if you paste the URL of your file into the W3C feed validator: http://validator.w3.org/feed/

It should say that your document is a valid feed! You might get some warnings, as it recommends that you also include some optional RSS elements, but your feed should work as it is.

Work together!

Web feeds are all about sharing data with others, so the best way to test your feed is to work in pairs! Get another student to consume your feed on his/her website, and in return consume his/her feed on your website. It’s all about collaboration!