Sunday, June 7, 2009

Joomla! Displaying Links to Blogger posts.

My website, http://www.kimenye.com, is powered by Joomla! (one of the world's most popular open source content management system). Not being a fun of its usability as a blogging platform, I made the decision to host this blog in Google's Blogger service. Having done that, I found I needed to display links to my blog from my website in a dynamic manner, that didn't require me to manually add links when I add new content. As part of my website, I have a panel where I intend to display the Titles and Links to the blog posts. This article describes how I achieved this.

Pre-Requisites
  1. Apache Web Server (preferably 2.0)
  2. PHP 5
  3. Zend GData PHP Library

Configuration of Zend PHP Library
We will be connecting to the Google Data Services to retrieve posts for the blog. This is done using the Zend GData PHP Library.  See this article on the Google Code pages with detailed information on how to configure the libary. There's also a video on the page by one of Google's Software Engineers with step by step instructions. If you are on shared hosting, your mileage may vary following those instructions. I'll outline the failed instructions briefly.
  1. Upload the Zend Framework library. From the unpacked archive, we need everything in the folder library (i.e the folder XXX\library\Zend).
  2. Edit the .htaccess file to include the Zend Library folder in the include path. 
This did not work for me, as my hosting provider has moved to using PHPSUEXEC and hence don't allow any php directives in the .htaccess file. These have to be moved to a php.ini file created in your public_html folder. I did this, however, this php.ini file was not getting parsed and hence my includes were failing. I checked my configuration using the phpinfo() method and saw that new php.ini file was not being parsed and hence the include_path variable was not being updated.

Using the Zend GData Library

  1. <?php
  2. // no direct access
  3. defined('_JEXEC') or die('Restricted access');
  4. //This is the path of where you have uploaded your Zend GData library
  5. $path = '/home/your_user_name/public_html/libraries/';
  6. //This method updates the include path to include the Zend GData libaries
  7. //This is useful if you are in a shared hosting environment where it is not
  8. //easy to update the include path
  9. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  10. //require the Zend Loader file
  11. require_once (dirname(__FILE__).DS.'Zend'.DS.'Loader.php');
  12. /**
  13. * @see Zend_Gdata
  14. */
  15. Zend_Loader::loadClass('Zend_Gdata');
  16. /**
  17. * @see Zend_Gdata_Query
  18. */
  19. Zend_Loader::loadClass('Zend_Gdata_Query');
  20. /**
  21. * @see Zend_Gdata_ClientLogin
  22. */
  23. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  24. /**
  25. * This is the class to use to connect to the Google Blogger Service
  26. */
  27. class BloggerHelper
  28. {
  29. function getList()
  30. {
  31. $count = 5; //this is the number of items to be returned
  32. $blogId = 123456789; //this is your blog id
  33. $email = 'someone@gmail.com'; //This is the email to log into Blogger
  34. $password = 'password'; //This is the password to log into Blogger
  35. $client = Zend_Gdata_ClientLogin::getHttpClient($email, $password, 'blogger');
  36. $gdClient = new Zend_Gdata($client);
  37. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/' . $blogId . '/posts/default');
  38. $query->setMaxResults($count);
  39. $feed = $gdClient->getFeed($query);
  40. $i = 0;
  41. $lists = array();
  42. foreach($feed->entries as $entry) //This is a temp hack to remove drafts
  43. {
  44. $lists[$i]->link = htmlspecialchars($entry->link[1]->text);
  45. $lists[$i]->text = $entry->title->text;
  46. $i++;
  47. }
  48. return $lists;
  49. }
  50. }

The code shown above requires you to know your blog id. This can be obtained by looking at the request parameters in the url for your blog. A simple way to find this is to log into your Blogger account, click on the blog you want to access and click on the settings link. Now look at the url on your blog. It should have something like "http://www.blogger.com/blog-options-basic.g?blogID=1234567890". The numbers after the string "blogID=" is your blog id. 

The getList() method returns an array of objects with the link as the url to the blog post and the text property as the title of the blog. This array of items is then used by a Joomla! module to display this information on the screen.

I've not found an elegant way to distinguish the drafts from the published posts, hence the current way of using the URL will have to do for now. In the future I intend to add some additional logic to do searching based on key words, so it works like an aggregator as well as functionality to display posts published in the last 30 days. Hope this is a nice starting place.

No comments: