Skip to content

Zend Framework: How-to interact with Google Calendar (Part 1/2)

Zend Framework has a great interface to access Google Data, this is one of the most easiest ways to implement Google Data API using PHP, in this short how-to i will explain how to authenticate, read and write information from/into your Google Calendar, Zend Framework Documentation has great examples on this. On this first part of this how-to i will explain how-to:

  1. Authenticate on Google Calendar
  2. List all your calendars
  3. Create a new Calendar

I will assume that you are already familiarized with the Zend Framework, and the project is already set up, meaning, include path’s and all extra work.

First let’s include the needed packages into our code:

 
<?php
require_once 'Zend/Gdata.php';
require_once 'Zend/Loader.php';
 
//Let's enable autoload, ZF handles this nicely
Zend_Loader::registerAutoload();
?>

In order to communicate with your Google Calendar, you need first to Authenticate, and there are 3 ways you can use to authenticate: ClientAuth, AuthSub and MagicCookie, you can read the details of each on the Zend Framework Documentation page, on my quick how-to i will use the ClientAuth method.

Authenticating on Google Calendar

 
/**
* Please define your Email and Password for Google Accounts access
*/
define('YOUR_EMAIL_FOR_GOOGLE_ACCOUNTS','');
define('YOUR_PASS_FOR_GOOGLE_ACCOUNTS','');
 
require_once 'Zend/Gdata.php';
require_once 'Zend/Loader.php';
 
//Let's enable autoload, ZF handles this nicely
Zend_Loader::registerAutoload();
$myEmail = YOUR_EMAIL_FOR_GOOGLE_ACCOUNTS;
$myPass  = YOUR_PASS_FOR_GOOGLE_ACCOUNTS;
 
// Parameters for ClientAuth authentication
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($myEmail, $myPass, $service);

Now that we have the authenticated client instance, we are able to interact with Google Calendar. The first action that i do, is to verify if the Calendar i want to write on, exists, if not, i create a new calendar.

Listing calendars and creating a new Calendar

/**
* Lets create the Service instance, to interact with Google Data Api
*/
$service = new Zend_Gdata_Calendar($client);
 
$calendarName = 'My Calendar ZF Example';
$useCalendarFeed = FALSE;
 
try {
// Let's get the calendar list from Google calendar API
$listFeed= $service-&gt;getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e-&gt;getMessage();
}
 
/**
* Need to run through all the array to search for our calendar name
* Calendar Listing Feed is available on $listFeed variable
*/
 
foreach ($listFeed as $calendar) {
if($calendar-&gt;title == $calendarName)
$useCalendarFeed = $calendar-&gt;content-&gt;src;
}
 
/**
* If we have not found the calendar we want to use,
* we need to create a new one
*/
if(FALSE === $useCalendarFeed)
{
$appCal = $service-&gt;newListEntry();
$appCal-&gt;title = $service-&gt;newTitle($calendarName);
$own_cal = "http://www.google.com/calendar/feeds/default/owncalendars/full";
$service-&gt;insertEvent($appCal, $own_cal);
 
/**
* Need to grab the Google Calendar feed again, with the newly inserted calendar
* We'll need the refreshed feed in order to get the newly added calendar ID in order
* to add the event into the created calendar
*/
try {
$listFeed= $service-&gt;getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e-&gt;getMessage();
}
 
}

If you go to your Google Calendar account a new calendar named “My Calendar ZF Example” was created.

You can download all the code from my Google Code Repository – Zend Framework Google Calendar Example 1/2.

Hope this helped, on the next how-to i will explain how to create events in your newly created calendar.

Published inCodingRandom Thoughts

12 Comments

  1. Simon Simon

    I have problem:

    and then I got error:
    Parse error: syntax error, unexpected ‘@’ in C:\xampp\htdocs\asd\cal02.php on line 11

  2. Thanks for the post, if anyone else wants to get rid of the error notices for the autoloader because you are using zf > 1.8 using below worked for me:

    //Let’s enable autoload, ZF handles this nicely
    //Zend_Loader::registerAutoload();
    require_once ‘Zend/Loader/Autoloader.php’;
    $loader = Zend_Loader_Autoloader::getInstance();

  3. Anonymous Coward Anonymous Coward

    Very informative article – code worked perfectly. I checked the API docs for Zend_Gdata_Calendar (http://framework.zend.com/apidoc/core/Zend_Gdata/Calendar/Zend_Gdata_Calendar.html) and don’t see newListEntry anywhere in there. Am I looking in the wrong place?

    Also do you have a code sample to delete a calendar?

    Lastly, where did your URL come from for the second argument in insertEntry() ?

  4. Ashok Ashok

    Excellent post – It made my calendar creation easy. Thanks for the post!!

  5. del del

    thanks for that!
    just started part of my website that deals with calendars, but before i go further, I’m thinking i should maybe let google take care of all this!

  6. Rick Rick

    Part 1/2 implies there’s going to be a part 2(yea, really!). It has been over 4 months now since part 1 was posted.

  7. Joselito Joselito

    Hi,

    Do you have a sample of how to delete non primary google calendar? I had seen 1 site that teaches how to delete google calendar however it didn’t works on my end. If you can you post it please. Thanks

  8. Dhaval Dhaval

    Hi,

    I really need part 2. Else somebody please post the message that how to update an event in secondary calendar using Zend framework

    I really appreciate your efforts and time.

    Thank you very much.

  9. Dhaval Dhaval

    Hello,

    I have found the way to update event in secondary calendar.

    There can be only one “organizer” as attendee in a meeting.
    You just need to add one guest as following

    Thanks a lot

  10. Dhaval Dhaval

    $oEvent->who[] = $service->newWho({URLDECODED_CALENDAR_ID}, “http://schemas.google.com/g/2005#event.organizer”, {CALENDAR_NAME});

Leave a Reply to Simon Cancel reply

Your email address will not be published. Required fields are marked *