So I was playing around with the Google Calendar portion of the Gdata API the other day and did some searching and found that there wasnt a Codeigniter library for it, probably because it seems that Google has teamed up with the guys that are working on the Zend Framework to bring Gdata to the PHP world. So I took the ZendGdata API for Google Calendar and implemented it in Codeigniter so that you just need to make a few simple function calls to gain authorization to a calendar, add events, query events, etc. This is my first attempt at writing a "library" for something, so hopefully it turned out well. If you find any errors in it or have any suggestions for improvement, just let me know!
Files you’ll need:
- Codeigniter-Gcal (Link fixed)
- ZendGdata
I also have it managed through a Git repository on on Github. Feel free to clone it or pull from it.
Setup:
First off we need to edit your config file. Open up your application/config/config.php file. Scroll down to the uri_protocol option and change it to PATH_INFO. Then go to the uri_allowed_chars setting and place a question mark after ‘a-z’. This allows you to put question marks in the URL. That?s it!
Place the Gcal.php file in your Codeigniter syste/application/libraries direcetory
Install the ZendGdata library with one of the two options:
* Make a directory anywhere on your machine/server
- Place the ZendGdata directory in it
Open you php.ini file and find the include_path line.
* Add to the include_path the directory that you placed the ZendGdata directory in. `/your/directory/ZendGdata/library`
- Save and restart Apache
Place the ZendGdata directory where you want.
In the Gcal.php library file, modify the require_once so that it points at the directory where ZendGdata is located. IE:
require_once(\"/your/directory/ZendGdata/Library/Zend/Loader.php\");
Place the calendar.php file in your controllers directory
The calendar.php file is a sample controller. In it you’ll find an example of a call to each function in the library. Also with each one, I go through and show how to manipulate the data returned since it can get a little confusing at times (the return values are often many-dimensional arrays that are a bit difficult to interpret just by looking at them).
AuthSub
AuthSub is basically Googles version of OAuth. Like OAuth, AuthSub sends the user to log into their Google account and then returns them to a specified URL with an access token as a GET variable.
<?php//AuthSub Authenticationfunction authSubLogin(){
$redirect = "/index.php/calendar/gCalDemo";
$this->load->library('Gcal');
$authSubUrl = $this->gcal->getAuthSubUrl($redirect);
echo "<a href=\"".$authSubUrl."\">login to your google account</a>";
}
?>
ClientLogin
ClientLogin is your basic, straight forward authentication with a users Google account username and password. It’s usually used for installed applications, but I included it anyway if you want to use it.
<?php//Client loginfunction clientLogin($username, $password){
$this->load->library('Gcal');
$this->gcal->clientLogin($username, $password);
redirect('controller/to/redirect/to');
}
And finally here is a list of the functions included in the library:
<?php//get list of calendars
$calFeed = $this->gcal->outputCalendarList($client);
//get events
$eventFeed = $this->gcal->outputCalendarEvents($client);
//get events events by date range
$eventQuery = $this->gcal->calEventsByDateRange($client, $startDate, $endDate);
//get events from text query
$eventFullQuery = $this->gcal->calFullTextQuery($client, $query);
//create a new event
$eventArray = array('title' => 'My test event',
'desc' => 'This is a description for my test event',
'where' => 'My house',
'startDate' => '2009-06-16',
'startTime' => '22:00',
'endDate' => '2009-06-17',
'endTime' => '04:00',
'tzOffset' => '-04'
);
$this->gcal->createEvent($client, $eventArray);
//create quick event
$eventInfo = "Dinner at my house next tuesday at 9pm";
$this->gcal->createQuickEvent($client, $eventInfo);
?>