[HowTo] Google Calendar API : PHP (Part 1)
Google Calendar is another indispensable tool from mighty Google. As with other Google tools, Calendar too have an API which lets users to manage the events remotely from within their web applications. Google Calendar API request/response works in the form of Google Data API feeds. The popular PHP client library used to work with Calendar service is Zend's GData which is also distributed as a part of Zend Framework. In this tutorial we will see how to install GData, authenticate against calendar servers and then access Calendar API to retrieve a list of calendars.
Install GData
First download the latest release GData library from here. Extract it and keep the folder anywhere you like. Lets assume that we kept it at /home/abbas/ZendGdata/.
Next step is to make sure you can include ZendGdata/library in your php script. This is done by setting include_path directive and this can be done in the following three ways:
- Permanently set the include_path directive in your php.ini configuration file from the command line
- Set the include_path path variable on a "per directory" level using .htaccess
- Use the set_include_path() function to dynamically set the include path in your scripts
We will use the set_include_path() function to set the include path. The code will look something like this...
-
$path = '/home/abbas/ZendGdata/library';
-
// Append the library path to existing paths
Authentication
The Gdata libarary can be used to access both public as well as private calendars. For private calendars we need to authenticate to the calendar servers. There are three types of authentication viz AuthSub proxy authentication, ClientLogin username/password authentication and Magic cookie authentication. We will be using ClientLogin username/password authentication in this tutorial as it is much easier to use and fits in the example we are going to take. Here's how to authenticate using username/password (Google calendar account credentials):
-
$path = '/home/abbas/ZendGdata/library';
-
-
require_once 'Zend/Loader.php';
-
Zend_Loader::loadClass('Zend_Gdata');
-
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
-
Zend_Loader::loadClass('Zend_Gdata_Calendar');
-
-
// User whose calendars you want to access
-
$user = 'username@gmail.com';
-
$pass = 'yourpass';
-
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
-
-
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
At this point authentication token is retrieved and $client is an object of Zend_Http_Client with appropriate Authentication header.
Retrieving list of Calendars
Now lets use the http client object to retrieve a list of all calanders.
(In continuation of above code..)
Voila!! Above code will output the name of all your calendars.
Next we will see how to retrieve events.
About this entry
You’re currently reading “ [HowTo] Google Calendar API : PHP (Part 1) ,” an entry on SANIsoft – PHP for E Biz
- Published:
- 4.26.10 / 11:46am
- Category:
- Google Calendar, PHP, Zend Framework
- Author:
- Abbas Ali
2 Comments
Jump to comment form | comments rss | trackback uri