Alright, first to get some things clear before I get a super super ass load of dick head responses about how this could be better, this is an intro! Here is what I have done. I have created a small script that gets my Buzz; this script will eventually include all of the functions of the Google Buzz API, or at least most of them. For now it includes what I have, minus a few things. They are sending me all the enclosures, but I am going to go ahead and just use SimplePie’s get_enclosures() method and calling it good. The reason why is that you should research your own implmentations, that and I am too lazy to perfect this because without the location, search functionality, or a complete understanding of PubSubHubBub (PuSH) I don’t feel the need to spend my time on perfecting it or doing anything more than an intro to it.
To get a few things straight, ease up on Google Buzz. I am not a spokesmen, salesmen, or in anyway paid to promote Buzz at all. I am only trying to give them a chance. If you are a web developer and you don’t like Google Buzz, you are not paying attention. Google Buzz, while it might be arguabaly commercial, is the bet attempt and including some great standard into the major web offerings ever. Google Buzz’s API page lists the standard that they have deicded are the futures. Some of them are exciting, others are just so-so. In my opinion the two that are the most exciting, and should be implemented as a standard, are Activity Streams and of course PubHubSubBub (not PubSubHubBub
). These are the future I think, I could be wrong and so could Google but these are great ideas and should be supported and explored by us all. I am sure someone will point out some point that will make me look like a Google fan boy and mention funding, etc… from Google. Well I don’t care, ask yourself if you had the money to waste and knew something was a great idea would you not throw some money as research and development too?
Sooooooo, ok, lets get started on it and get some basics out of the way. To prepare we need a few standard things, these should be kind of obvious from the title but here goes:
- Download SimplePie – http://github.com/rmccue/simplepie/downloads
- (optional) Check out the SimplePie Developer Wishlists
at http://bit.ly/9sZc9L or http://bit.ly/9YAjD7 or http://bit.ly/9CCsuG - Alright we got the SimplePie class and we have thanked the developers, really, at least tweet that you use their awesome library!
- In all fairness, if I haven’t done good enough so far, you should read at least some of the SimplePie documentation. I will go ahead and be the first to say I haven’t read all of it. I have read what I needed and also checked out their tutorials. All I have to say is that they have done an excellent job, only on a few occasions have I not been able to find what I want, so go read it and see what you learn. You will be amazed at what SimplePie does that you would never imagine that these guys would have thought of.
- Go ahead and start a new *.php(name it whatever you want) file and open the php tags and include SimplePie
<?php include('/classes/SimplePie.class.php'); ?> - There is a standard opening of the feed for SimplePie, it goes as follows and it will basically create your new object instance and let it know which feed to read. Make sure to replace the {username} tag with your username, or anyone else you would like to feature with Buzz
-
$feed_url = 'http://buzz.googleapis.com/feeds/{username}/public/posted'; $feed = new <a href="http://simplepie.org/" title="SimplePie">SimplePie</a>(); $feed->set_feed_url($feed_url); $feed->set_cache_duration($cacheDuration); $feed->init();
-
- In a standard SimplePie way you need to check for a feed error before we move on to parsing the Google Buzz feed
-
if($feed->error()) { echo '<div class="error">' . $feed->error() . '</p>'; }
-
- Now if we don’t have an error we can look for some content. If you read the SimplePie documentation like I mentioned earlier then you know a few of the commands that we will be using here. If you haven’t read the documentation then go do it. I know that a lot of peeps like to copy-paste their way through life, but really bear with me and try to understand this one. SimplePie is useful in almost any site and I need to spend more time with it too so go read those docs.
-
echo '<a href="' . $item->get_link() . '">' . $item->get_title() . '</a>';
-
- So now we have printed out an anchor tag with and title from each Buzz from the user and also the right link to that Buzz post. Next we need to handle if their are any media elements we should display. This is one of the things that I have skimped on a bit. I really haven’t perfected this because they are using the Yahoo Media RSS namespace and I don’t want to get into this yet. It is simple actually and I might do another post on it, or if you want to post a comment on the code go ahead. I just want to get the basics out of the way. So for now lets use SimplePie and just print out the enclosure links. I know that this will print out each enclosure twice, remember that this is just the beginning.
-
if($item->get_enclosures()) { echo '<br /><h3>Enclosures</h3><br />'; foreach($item->get_enclosures() as $enclosure) { echo "<p>" . $enclosure->get_link() . "</p>"; } }
-
- The next part is one of the more interesting parts of SimplePie to me, I said these guys thought of everything! The developers put in a way to get namespace elements from other feed namespaces if you specify the namespace. Well really you don’t even have to specify any namespace at all! You can just use this function to get things from RSS feeds as you need them. So using the get_item_tags() method we can get any namespaced, or not, element we want. In this case we are going to use the purl.org thread namespace to get the number of replies to the original Buzz.
-
$total = $item->get_item_tags('http://purl.org/syndication/thread/1.0', 'total');
-
- So now we have that number we can use that to run our next conditional and see if we need to even try to fetch some comments.
-
if($total[0]['data'] > 0)
-
- So if that exists we need to get replies. I haven’t reall polished the reply fetching yet, and I don’t plan to anytime soon. You can do that part yourself! Well now we have the basics to actually start parsing the feed. I have some things that I haven’t included here yet. So below is the entire code of a sample page that I created to test the parsing of the Google Buzz response. It is really simple and basic. Don’t flame me for simplicity, realize that some people need to see it in this manner to get started.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Buzz Test</title>
<style type="text/css" media="screen">
div.item {
background: #efefef;
border: 2px solid #333;
margin-bottom: 20px;
}
div#wrapper {
width: 960px
}
</style>
</head>
<body>
<?php include('/classes/SimplePie.class.php'); //Include the SimplePie Library ?>
<div id="wrapper">
<?php
$feed_url = 'http://buzz.googleapis.com/feeds/{username}/public/posted';
$feed = new SimplePie();
$feed->set_feed_url($feed_url);
$feed->set_cache_duration($cacheDuration);
$feed->init();
if($feed->error())
{
echo '<div class="error">' . $feed->error() . '</p>';
}
else
{
foreach($feed->get_items() as $item)
{
echo "<div class='item'><ul>";
echo '<li><a href="' . $item->get_link() . '">' . $item->get_title() . '</a></li>';
echo '<li>' . $item->get_date() . '</li>';
echo '<li>' . $item->get_content() . '</li>';
if($item->get_enclosures())
{
echo '<h3>Media</h3>';
foreach($item->get_enclosures() as $enclosure)
{
echo '<li>' . $enclosure->get_link() . '</li>';
}
}
$total = $item->get_item_tags('http://purl.org/syndication/thread/1.0', 'total');
if($total[0]['data'] > 0)
{
$links = $item->get_item_tags('http://www.w3.org/2005/Atom', 'link');
foreach($links as $link)
{
if($link['attribs']['']['rel'] == 'replies')
{
$replyFeed = new SimplePie();
$replyFeed->set_feed_url($link['attribs']['']['href']);
$replyFeed->set_cache_duration($cacheDuration);
$replyFeed->init();
if($replyFeed->error())
{
echo '<p>' . $replyFeed->error() . '</p>';
}
else
{
echo '<h3>Replies</h3>';
foreach($replyFeed->get_items() as $replyItem)
{
echo '<li>' . $replyItem->get_content() . '</li>';
}
}
}
}
}
echo "</div></ul>";
}
}
?>
</div>
</body>
</html>
That code is gross, well at least it is to me. You can use way more xhtml and way less php echos which will increase your readability a whole lot! This is a very basic intro, so enjoy it, play with it, change it, see what you can get! If you have any questions then please post your code and what you need to know and I’ll see if I can help at all. If I can’t then maybe a reader can.












Recent Comments