Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2008
    Location
    Orlando, FL
    Posts
    60
    Thanks
    3
    Thanked 3 Times in 3 Posts

    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.

  • #2
    New Coder
    Join Date
    Jul 2005
    Posts
    54
    Thanks
    2
    Thanked 1 Time in 1 Post
    A good little parser is MagicParser.

    Check it out here.

    Hope it helps

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,455
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    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.

  • #4
    New Coder
    Join Date
    May 2008
    Location
    Orlando, FL
    Posts
    60
    Thanks
    3
    Thanked 3 Times in 3 Posts
    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.

  • #5
    New Coder
    Join Date
    May 2008
    Location
    Orlando, FL
    Posts
    60
    Thanks
    3
    Thanked 3 Times in 3 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •