rss20: Oops! You need to pass a URL!"; return false; } /* Use http object to retrieve raw RSS XML and to cache the data. Review class_http at http://www.troywolf.com/articles/class_http/ */ $h = new http(); $h->dir = "../../../../../cache/"; if (!$h->fetch($url, $ttl)) { /* The class has a 'log' property that contains a log of events. This log is useful for testing and debugging. */ echo "

There is a problem with the http request!

"; echo $h->log; exit(); } /* Use xml object to parse the raw RSS XML. Review class_xml at http://www.troywolf.com/articles/class_xml/ */ $x = new xml(); if (!$x->fetch($h->body)) { /* The class has a 'log' property that contains a log of events. This log is useful for testing and debugging. */ echo "

There was a problem parsing your XML!

"; echo $x->log; exit(); } /* Some debugging help. */ #echo "
"; #echo $h->log; #echo "
"; #echo $x->log; #echo "
\n";
  #print_r($x->data);
  #echo "
\n"; /* Now that we have the RSS data parsed into an object, here is how to work with it. */ #$version = $x->data->RSS[0]->_attr->VERSION; #$channel_link = $x->data->RSS[0]->CHANNEL[0]->LINK[0]->_text; #$channel_title = $x->data->RSS[0]->CHANNEL[0]->TITLE[0]->_text; echo "
\n"; echo "
\n"; echo "data->RSS[0]->CHANNEL[0]->LINK[0]->_text ."\">data->RSS[0]->CHANNEL[0]->IMAGE[0]->URL[0]->_text."\"" ." alt=\"".$x->data->RSS[0]->CHANNEL[0]->TITLE[0]->_text."\" />"; echo "
\n"; $total_items = count($x->data->RSS[0]->CHANNEL[0]->ITEM); if ($count == 0 || $count > $total_items) { $count = $total_items; } for ($idx=0; $idx<$count; $idx++) { $item = $x->data->RSS[0]->CHANNEL[0]->ITEM[$idx]; echo "
\n"; echo "LINK[0]->_text ."\">".$item->TITLE[0]->_text."\n"; echo "
".$item->DESCRIPTION[0]->_text."
\n"; echo "
".$item->PUBDATE[0]->_text."
\n"; echo "
\n"; } echo "
\n"; } ?> RSS Example using class_http and class_xml

RSS Example using class_http and class_xml

Author: Troy Wolf (troy@troywolf.com)
Modified Date: 2005-06-27 16:25

The column shown on the right contains the top 5 news items from Google News. The news is syndicated using RSS. Thousands of websites from major news sites such as Yahoo and NPR to expert sites such as techreport.com to personal blogs provide their content in the RSS format making it easy for you to incorporate their content on your own website.

This page is a demonstration of the rss20() function that combines the power of two PHP classes from Troy Wolf:
  1. class_http.php
  2. class_xml.php
View the source of this page which includes the rss20() function.

About the author

Troy Wolf is the author of SnippetEdit--a PHP application providing browser-based website editing that even non-technical people can use. Website editing as easy as it gets. Troy has been a professional Internet and database application developer for over 12 years. He has many years' experience with ASP, VBScript, PHP, Javascript, DHTML, CSS, SQL, and XML on Windows and Linux platforms. Check out Troy's Code Library.