This tutorial shows you how to read Google SideWiki comments with PHP.
DOWNLOAD THE SOURCE CODE
SideWiki is a new service from Google that allows users to add comments to any web page. The Google Toolbar provides a convenient interface to SideWiki, but there is an API that web developers can use to access SideWiki data. This tutorial shows you how to read SideWiki posts using PHP.
Initially I thought I would use the SimplePie RSS & Atom parser to read the results from the SideWiki API. It was a reasonable assumption, given that "[Google Data API] Feeds conform to either the Atom or RSS syndication formats". I was wrong.
After a lot of mucking around I found that the GData feeds are not read by quite a few feed parsers. It seems that, while GData may utilize the extension mechanisms of feed formats like RSS and Atom, in doing so they break comparability with some popular feed parsers.
Google does have a PHP library capable of dealing with GData feeds. It is called the Google Data PHP Client Library, and you can download a copy from here. Extract it and add a reference to the library from your PHP.INI file, using the include_path option.
In a real world application you would read SideWiki posts from the current page. For the purposes of this demo we will hard code a page that is known to have some SideWiki posts.
$sourceURL = "http://googleblog.blogspot.com/2009/09/help-and-learn-from-others-as-you.html";
$sideWikiURL = "http://www.google.com/sidewiki/feeds/entries/webpage/" . rawurlencode($sourceURL) . "/full";
In order to use the classes included in the Google Data PHP Client Library, the Loader.php file has to be loaded along with the Zend_Gdata, Zend_Gdata_Query and Zend_Gdata_Feed classes.
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Feed');
The following code is used to contact the SideWiki API REST server and save the results in a variable called feed.
$gdClient = new Zend_Gdata($client);
$query = new Zend_Gdata_Query($sideWikiURL);
$feed = $gdClient->getFeed($query);
Here we display the returned results as simple paragraph elements, with the title also being a clickable link.
foreach($feed->entries as $entry)
{
echo '<p><a href="'. $entry->link . '">' . $entry->title->text . '</a></p>';
echo '<p>' . $entry->content->text . '</p>';
}
With a library that can parse the GData feeds, accessing and displaying SideWiki entries is very simple, and anyone who has written a RSS or Atom display will have no trouble working with the SideWiki API.
Return to the Tutorial Index