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.
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.
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.
<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