Skip to main content

blogger feed

The good people at the seminar center I volunteer for have a newsletter on blogger. I persuaded them to use blogger instead of crafting something in open office and then waste some money on poststamps. The bonus is that more people can contribute and update.
They also have a website with static pages. I wrote a quick hack to create a page containing the latest newsletter posts based on the atom feed that blogger provides.

reading the feed

reading the feed is eay: use the link on the blog like this

$xmlstr = file_get_contents('http://yourblog.blogspot.com/feeds/posts/default');

Create the xml object:

$xml = new SimpleXMLElement($xmlstr);

find the label of the latest post

The labels can be found by iterating the category tag of a post. The label is an attribute called 'term' and there are as many category tags as there are labels for a post.

$current = '';
$labels = $xml->entry->category;
foreach($labels as $l)
{
if (substr($l['term'],0,11) == 'nieuwsbrief')
{
$nr = substr($l['term'],11);
$current = $l['term'];
}
}

Now $current holds the labelname we need.

retrieving the posts

All we need to do now is iterate the post and output the the ones with the correct label.

$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->entry as $title) {

//iterate the categories
  foreach($title->category as $l)
  {
  if (strcmp($l['term'], $current)== 0)
   {
   echo '<h2>'.$title->title.'</h2>';
   echo $title->content;
   }
  }
  $ndx ++;
}

I had to recreate the xml object to make that work, also you must use strcmp to compare strings since $l['term'] is treated as an object.

Don't forget to provide some css styling.

simpleXML
blogger API

Comments