AJAX - Good or Evil?
I was tired of living in the early 2000’s so I changed my kpages rss feed service to AJAX….
But lo and behold, I like it a lot better the way it was before. I used to load the entire page, and every feed all at once. When a user wanted to read a feed it would appear. Now it loads the first page REALLY fast, but loads the feeds a little too slow for my liking.
But you probably want to know how I did it.
- Write an RSS Parser. This was probably the biggest part, but also the most interesting. RSS feeds are very complex, so I had to interpret them in many different ways. When it was of type RDF I was able to strictly use the DOM functions that are built into PHP 5 to read them.
case 'rdf:RDF': #Stupid RSS $title = $dom->getElementsByTagName('title')->item(0)->nodeValue; $link = $dom->getElementsByTagName('link')->item(0)->nodeValue; $desc = $dom->getElementsByTagName('description')->item(0)->nodeValue; break;When it was an ATOM feed it was a little more tricky because so many sites’ feeds are uncompliant. I had to triple check to get the link back to the homepage. A like so…
case 'feed': #ATOM $title = $dom->getElementsByTagName('title')->item(0)->nodeValue; $link = $dom->getElementsByTagName('link')->item(0)->nodeValue; if($link == '') { $link_list = $dom->getElementsByTagName('link'); foreach($link_list as $link_node) { $link_type = $link_node->getAttribute('type'); $link_href = $link_node->getAttribute('href'); if($link_type == 'text/html') { $link = $link_href; break; } } } $desc = $dom->getElementsByTagName('tagline')->item(0)->nodeValue; break;Regular RSS feeds were the easiest to read. I even got to use the xpath functions of the DOM XML module. Piece of cake, really.
default: #Nice RSS $tmp_title = $xpath->query("/$type/channel/title"); $title = $tmp_title->item(0)->nodeValue; $tmp_link = $xpath->query("/$type/channel/link"); $link = $tmp_link->item(0)->nodeValue; $tmp_desc = $xpath->query("/$type/channel/description"); $desc = $tmp_desc->item(0)->nodeValue; break;
The next part of the parser is arguably the most important step, actually reading the feeds. I accomplished this by getting all of the “stories” from the rss
function get_items($dom) { switch($this->feed_type) { case 'feed': $item_name = 'entry'; $desc_name = 'content'; break; default: $item_name = 'item'; $desc_name = 'description'; } $items = $dom->getElementsByTagName("$item_name"); $item_list = array(); foreach ($items as $item) { $title = $item->getElementsByTagName('title')->item(0)->nodeValue; //First Try to get Link $link = $item->getElementsByTagName('link')->item(0)->nodeValue; //Try again if($link == '') { $link_list = $item->getElementsByTagName('link'); foreach($link_list as $link_node) { $link_type = $link_node->getAttribute('type'); $link_href = $link_node->getAttribute('href'); if($link_type == 'text/html') { $link = $link_href; break; } } } //Last try if($link == '') { $link_node = $item->getElementsByTagName('link')->item(0); $link = $link_node->getAttribute('href'); } $desc = $item->getElementsByTagName("$desc_name")->item(0)->nodeValue; $hash = array( 'title' => $title, 'link' => $link, 'desc' => $desc ); array_push($item_list, $hash); } return $item_list; }

