You are here:Home » Ajax » RSS Feed Reader using Ajax

RSS Feed Reader using Ajax


SIMPLE XML FEED READER

This RSS feed reader written in PHP can be easily customised and is very useful for news posting sites or sites whose content constantly change. Another benefit of using RSS feed reader is that it can improve your SEO rank. Without wasting time I am giving out this tutorial free.  The code here is originally from kidslovepc. I love it because its quick, simple and easily customisable. Be relaxed, sit back and enjoy. Have any question? Be free to ask me:

Download here

RSSFEED.XML:

<?xml version="1.0" ?>
<rss version="2.0">
<channel>

<title>How Rubbish engaged Mrs Smith</title>
<description>
Sunday night will forever remain special in the heart of Mrs Smith for bla bla bla bla bla 
</description>
<link>http://localhost/localnews/Mrs-smith-gets-new-heartrob</link>
<item>
<title>Fraudsters Scam AQuaeda spy </title>
<description>
The tale has not ended for the latest in stocks of stories of how cash is swindled.And so?
</description>
<link>http://localhost/localnews/local-scam</link>
</item>
<item>
<title>Obama Health Care: Final Court Verdict</title>
<description>
 Victory at last to many American groups opposed to the obama care as......
</description>
<link>http://localhost/localnews.php/obama-does-not-really-care</link>
</item>
</channel>
</rss>

RSS_FEED_READER - PHP CODE:

<?php
set_time_limit(0); //so that it does not timeout anyhow
$file = "http://localhost/rssfeed.xml";
$TOTALITEM_WANTED=5; //number of items to fetch
$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
global $rss_channel, $currently_writing, $main;
switch($name) {
case "RSS":  //RSS1.0
case "RDF:RDF": //RSS above 1.0
case "ITEMS":
$currently_writing = "";
break;
case "CHANNEL":
$main = "CHANNEL";
break;
case "IMAGE":
$main = "IMAGE";
$rss_channel["IMAGE"] = array();
break;
case "ITEM":
$main = "ITEMS";
break;
default:
$currently_writing = $name;
break;
}
}
function endElement($parser, $name) {
global $rss_channel, $currently_writing, $item_counter;
$currently_writing = "";
if ($name == "ITEM") {
$item_counter++;
}
}
function characterData($parser, $data) {
global $rss_channel, $currently_writing, $main, $item_counter;
if ($currently_writing != "") {
switch($main) {
case "CHANNEL":
if (isset($rss_channel[$currently_writing])) {
$rss_channel[$currently_writing] .= $data;
} else {
$rss_channel[$currently_writing] = $data;
}
break;
case "IMAGE":
if (isset($rss_channel[$main][$currently_writing])) {
$rss_channel[$main][$currently_writing] .= $data;
} else {
$rss_channel[$main][$currently_writing] = $data;
}
break;
case "ITEMS":
if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
} else {
$rss_channel[$main][$item_counter][$currently_writing] = $data;
}
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
// output HTML
print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>");
if (isset($rss_channel["ITEMS"])) {
if (count($rss_channel["ITEMS"]) > 0) {
for($i = 0;$i < $TOTALITEM_WANTED;$i++) {
if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
}
else {
print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
}
print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />"); }
} else {
print ("<b>There are no articles in this feed.</b>");
}
}
?>
  • Feed Reader, XML Reader, RSS Reader Script, PHP Feed Reader

0 comments:

Post a Comment