Bright Hub
 
Matthew Casperson's Hubfolio

Access SideWiki comments with PHP

Article by Matthew Casperson (4,923 pts )
Published on Nov 10, 2009

This tutorial shows you how to read Google SideWiki comments with PHP.

Introduction

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.

Google SideWiki and PHP Screenshot

Step 1 - GData != Atom

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.

Step 2 - Get the Google Data PHP Client Library

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.

Google SideWiki and PHP Screenshot

Step 3 - Define the REST URLs

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";

Step 4 - Load the ZEND classes

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');

Step 5 - Query the API

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);

Step 6 - Display the results

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>';

}

Conclusion

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

Search More About:

 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape