Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2008, 09:40 PM   PM User | #1
woop
New Coder

 
Join Date: May 2008
Location: Orlando, FL
Posts: 60
Thanks: 3
Thanked 3 Times in 3 Posts
woop is an unknown quantity at this point
PHP to Parse Blog XML for Latest Posts?

I've been searching around the web everywhere for a PHP script that would allow me to list my 3 latest Blogger posts according to the XML Blogger makes. I can't find anything. The only thing I could find was a page that said I would need to use a Magpie library that isn't available for download.

Example of what I mean - Near the bottom of http://www.dreamten.com/ - although that's WordPress.

Any ideas where I can find such a script?

Thanks!
Simon.

Edit: Found a website that offers a Javascript solution similar to what I'm looking for, but that can't possibly be the only way, right? If the site goes down then a part of mine does too. Not that great a solution.

Last edited by woop; 11-21-2008 at 12:33 AM..
woop is offline   Reply With Quote
Old 11-21-2008, 01:49 AM   PM User | #2
lesmith
New Coder

 
Join Date: Jul 2005
Posts: 54
Thanks: 2
Thanked 1 Time in 1 Post
lesmith is an unknown quantity at this point
A good little parser is MagicParser.

Check it out here.

Hope it helps
lesmith is offline   Reply With Quote
Old 11-21-2008, 05:09 AM   PM User | #3
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Give me the URL to the XML file you want to read (the RSS feed).

The solution is a PHP script that's very easy to do, but need to
see the XML in order to provide an example.

And yes, if their site goes down, the RSS feed won't show.
Not much you can do about that. If that's a recurring problem,
then you quit using their feed.


.

Last edited by mlseim; 11-21-2008 at 05:11 AM..
mlseim is offline   Reply With Quote
Old 11-25-2008, 05:42 AM   PM User | #4
woop
New Coder

 
Join Date: May 2008
Location: Orlando, FL
Posts: 60
Thanks: 3
Thanked 3 Times in 3 Posts
woop is an unknown quantity at this point
Thanks for the replies. After frustrating hours I finally figured it out with a script from Sitepoint.com (http://www.sitepoint.com/examples/ph...ver-oo.php.txt).

Here is the code I'm using (although I'm going to style it with CSS). Hopefully it helps someone else out there who's been wanting to parse their Blogger RSS. This one will list articles from a Blogger RSS as format:
  • <a href='URL'>Date. Title</a></li>
  • <a href='URL'>Date. Title</a> (1 comment)</li>
  • <a href='URL'>Date. Title</a> (2 comments)</li>

Code:
<ul>
<?php

class RSSParser {

	var $insideitem = false;
	var $tag = "";
	var $title = "";
	var $pubDate = "";
	var $link = "";
	var $thr = "";
	var $counter = 1;

	function startElement($parser, $tagName, $attrs) {
		if ($this->insideitem) {
			$this->tag = $tagName;
		} elseif ($tagName == "ITEM") {
			$this->insideitem = true;

		}
	}

	function endElement($parser, $tagName) {
		if ($this->counter < 5) {
			if ($tagName == "ITEM") {
				$date = htmlspecialchars(trim($this->pubDate));
				$pubDate = substr($date,0,str_length-20) . ".";
				printf("<li><a href='%s'>%s %s",trim($this->link),$pubDate,htmlspecialchars(trim($this->title)));
					if ($this->thr == 1) { printf(" (%s comment)",$this->thr); }
					elseif ($this->thr > 1) { printf(" (%s comments)",$this->thr); }
				echo "</a></li>";	
				$counter = $this->counter;
				$this->counter = $counter + 1;
				$this->title = "";
				$this->pubDate = "";
				$this->link = "";
				$this->thr = "";
				$this->insideitem = false;
				}
			}
	}

	function characterData($parser, $data) {
		if ($this->insideitem) {
		switch ($this->tag) {
			case "TITLE":
			$this->title .= $data;
			$this->counter .= $counter;
			break;
			case "PUBDATE":
			$counter++;
			$this->pubDate .= $data;
			break;
			case "LINK":
			$counter++;
			$this->link .= $data;
			break;
			case "THR:TOTAL":
			$counter++;
			$this->thr .= $data;
			break;
		}
		}
	}
}

$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("rss.xml","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);

?>
</ul>
Please post here if you run across this code and it helps you. I'd like to know that all of my frustration saved someone else's time. Lol, I should have just hired a coder.

Last edited by woop; 11-26-2008 at 06:01 PM..
woop is offline   Reply With Quote
Old 11-29-2008, 09:00 PM   PM User | #5
woop
New Coder

 
Join Date: May 2008
Location: Orlando, FL
Posts: 60
Thanks: 3
Thanked 3 Times in 3 Posts
woop is an unknown quantity at this point
This is probably more helpful:

Code:
<div id="latestentries">
	<ul id="entrieslist">
<?php

class RSSParser {

	var $insideitem = false;
	var $tag = "";
	var $title = "";
	var $pubDate = "";
	var $link = "";
	var $thr = "";
	var $counter = 1;

	function startElement($parser, $tagName, $attrs) {
		if ($this->insideitem) {
			$this->tag = $tagName;
		} elseif ($tagName == "ITEM") {
			$this->insideitem = true;

		}
	}

	function endElement($parser, $tagName) {
		if ($this->counter < 5) {
			if ($tagName == "ITEM") {
				$title = htmlspecialchars(trim($this->title));
					if (strlen($title) > 35) {
						if ($this->thr > 0) {
							$name = $title;
							$name = $name . " "; 
							$name = substr($name,0,31);
							$title = $name . " ..."; 
						}
						if (strlen($title) > 45) {
							if ($this->thr == 0) {
								$name2 = $title;
								$name2 = $name2 . " "; 
								$name2 = substr($name2,0,41);
								$title = $name2 . "..."; 
							}
						}
					}
				$date = htmlspecialchars(trim($this->pubDate));
				$pubDate = substr($date,0,str_length-20) . ".";
				printf("<li class='entry'><a href='%s'><span class='entrydate'>%s</span><span>%s</span>",trim($this->link),$pubDate,$title);
					if ($this->thr == 1) { printf("<span class='entrycomments'>(%s Comment)</span>",$this->thr); }
					elseif ($this->thr > 1) { printf("<span class='entrycomments'>(%s Comments)</span>",$this->thr); }
				echo "</a></li>";	
				$counter = $this->counter;
				$this->counter = $counter + 1;
				$this->title = "";
				$this->pubDate = "";
				$this->link = "";
				$this->thr = "";
				$this->insideitem = false;
			}
		}
	}

	function characterData($parser, $data) {
		if ($this->insideitem) {
		switch ($this->tag) {
			case "TITLE":
			$this->title .= $data;
			$this->counter .= $counter;
			break;
			case "PUBDATE":
			$counter++;
			$this->pubDate .= $data;
			break;
			case "LINK":
			$counter++;
			$this->link .= $data;
			break;
			case "THR:TOTAL":
			$counter++;
			$this->thr .= $data;
			break;
		}
		}
	}
}

$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("../rss.xml","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);

?>
	</ul>
</div>
This code does this:
• process the latest 4 blog entries from Blogger's RSS.xml
• add the blog entry date
• detect titles greater than 35 characters.
- titles OVER 35 characters and WITH comment(s) are shortened to 31 characters + " ..."
- titles with NO comment and OVER 45 characters are shortened to only 41 characters + " ..."
• add "(1 Comment)" for 1 comment entries
• add instead "(2 Comments)" for 2+ comments entries
• add nothing for 0 comments on entries
woop is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:03 PM.


Advertisement
Log in to turn off these ads.