For reference, here’s the full code listing:
<?php include "db_ado_vars_test.inc.php"; //your database connection details include "$includes_path/adodb/tohtml.inc.php"; include "$includes_path/adodb/adodb.inc.php"; $conn = &ADONewConnection('mysql'); $conn->PConnect($host,$user,$pass,$db_name); $rssFeeds = array ('southafrica.rss','africa.rss'); //Loop through the array, reading the feeds one by one foreach ($rssFeeds as $feed) { readFeeds($feed); } $sql = "SELECT title, description, link, pubdate FROM rss_feeds ORDER BY pubdate DESC LIMIT 100"; $rs = &$conn->Execute($sql); while (!$rs->EOF) { echo ''.$rs->fields['title'].''.$rs->fields['pubdate'].'<br>'; echo $rs->fields['description'].'<br>'; $rs->MoveNext(); // Moves to the next row } function startElement($xp,$name,$attributes) { global $item,$currentElement; $currentElement = $name; if ($currentElement == 'ITEM') { $item = true; } } function endElement($xp,$name) { global $item,$currentElement,$title,$description,$link,$pubdate,$conn; if ($name == 'ITEM') { echo "Title: $title<br>"; echo "Description: $description<br>"; echo "Link: $link<br>"; echo "Pubdate: $pubdate<br><br>"; $ins_title = addslashes($title); $ins_desc = addslashes($description); $ins_link = addslashes($link); $ins_pubdate = addslashes($pubdate); $sql = "SELECT COUNT(link) as cn FROM rss_feeds WHERE link='$ins_link'"; $rs = $conn->Execute($sql); if ($rs->fields['cn'] == 0) { $sql = "INSERT INTO rss_feeds (title, description, link, pubdate) VALUES('$ins_title','$ins_desc','$ins_link','$ins_pubdate')"; if (!($conn->Execute($sql))) { print 'Error inserting: '.$conn->ErrorMsg().'<br>'; } } $title = ''; $description = ''; $link = ''; $pubdate = ''; $item = false; } } function characterDataHandler($xp,$data) { global $item,$currentElement,$title,$description,$link,$pubdate; if ($item) { switch($currentElement) { case "TITLE": $title .= $data; break; case "DESCRIPTION": $description.=$data; break; case "LINK": $link.=$data; break; case "PUBDATE": $pubdate.=$data; break; } } } function readFeeds($feed) { $fh = fopen($feed,'r'); // open file for reading $xp = xml_parser_create(); // Create an XML parser resource xml_set_element_handler($xp, "startElement", "endElement"); // defines which functions to call when element started/ended xml_set_character_data_handler($xp, "characterDataHandler"); while ($data = fread($fh, 4096)) { if (!xml_parse($xp,$data)) { return 'Error in the feed'; } } } ?>
It’s easy to extend this script to cater for multiple feeds, or break it up into categories if needs be. You’ll also most probably want to separate the reading and writing functions too. Reading a large number of remote feeds can be time-consuming, and not viable if your page is read by multiple people.
As always there are other ways – you can use PHP’s XSL functions, for example, as pointed out by Byron Adams in a response to last month’s article. Or as Jens Ulrik suggested, use an existing solution, such as Magpie-RSS. Hopefully you’ve seen just how easy it is to do this yourself, and just how simple RSS really is. Good luck!