Simple RSS reader based on PHP

Web & Code No Comments

There are many RSS parsers, readers and 3rd party scripts which do the work of reading a rss feed and displaying it on an external web page. When I ran into this problem recently, I decided to write for myself a plain simple code which could do the job without much of a hassle.

<?php
$startitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
	global $startitem, $tag, $title, $description, $link;
	if ($startitem) {
		$tag = $name;
	} elseif ($name == "ITEM") {
		$startitem = true;
	}
}
function endElement($parser, $name) {
	global $startitem, $tag, $title, $description, $link;
	if ($name == "ITEM") {
	printf("<dt><a href='%s'>%s</a></dt>",
		trim($link),htmlspecialchars(trim($title)));
	printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
		$title = "";
		$description = "";
		$link = "";
		$startitem = false;
	}
}
function characterData($parser, $data) {
	global $startitem, $tag, $title, $description, $link;
	if ($startitem) {
	switch ($tag) {
		case "TITLE":
		$title .= $data;
		break;
		case "DESCRIPTION":
		$description .= $data;
		break;
		case "LINK":
		$link .= $data;
		break;
	}
	}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("YOUR RSS LINK HERE","r")
	or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
	xml_parse($xml_parser, $data, feof($fp))
		or die(sprintf("XML error: %s at line %d",
		xml_error_string(xml_get_error_code($xml_parser)),
		xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>

P.S. The code is not entirely my own. I have referred Kevin Yank’s Tutorial . If you want to understand how the whole thing works, I suggest that you read through it.

Simple Ajax for Dynamic Webpages

Web & Code No Comments

Let us suppose you have a 5 page website with a basic header navbar and content. Traditionally, in Static websites, when one clicks on the navbar element to see a different content the whole header, navbar and the new content will be reloaded. This defiantly is not the most efficient way as there is unnecessary reloading of the header and navbar which remain same in all the webpages.  One good way of solving the problem is you PHP and make it interact with a database. But a db for a 5 page website is a bit too much.

One simple solution to this problem is using a combination of JS, XML and HTML/XHTML. Or what is today popularly known as AJAX- Asynchronous Java Script and XML.

The code

//inside the head element of the Homepage HTML
<script type="text/javascript">
function loadurl(link){
try {
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { //code for error handling
}
xmlhttp.onreadystatechange = triggered;
xmlhttp.open("GET", link);
xmlhttp.send(null);
}
function triggered(){
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
document.getElementById("content").innerHTML = xmlhttp.responseText;
}
}
</script>

//inside the nav bar element
<div id="nav">
<ul>
<li>
<a onclick="loadurl('foldername/contentname.html')">content title</a>
</li>
[...]
</ul>
</div>

//inside contentname.html
<html>
<head> </head>
<body>
<h2>Content Title</h2>
<p>Hello World</p>
</body>
</html>

Read the rest of this entry »

© 2009, Nikhil Hullur | Entries RSS Comments RSS