[HowTo] Google Calendar API : PHP – Manage events (Part 3)

Earlier we saw how to install Zend's GData library and retrieve list of calendars and events. Now its time to manage events i.e. create, update and delete events.

First we need the Calendar service instance, so lets instantiate the Zend_GData_Calendar class.

PHP:
  1. $path = '/home/abbas/ZendGdata/library';
  2. $oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  3. require_once 'Zend/Loader.php';
  4. Zend_Loader::loadClass('Zend_Gdata');
  5. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  6. Zend_Loader::loadClass('Zend_Gdata_Calendar');
  7. // User whose calendars you want to access
  8. $user = 'username@gmail.com';
  9. $pass = 'yourpass';
  10. $serviceName = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
  11. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $serviceName);
  12. $service = new Zend_Gdata_Calendar($client);

Creating Single Occurrence Events

Lets directly look at the code which has self explanatory comments

PHP:
  1. // Create a new event object using calendar service's factory method.
  2. // We will then set different attributes of event in this object.
  3. $event= $service->newEventEntry();
  4.  
  5. // Create a new title instance and set it in the event
  6. $event->title = $service->newTitle("Some Event");
  7. // Where attribute can have multiple values and hence passing an array of where objects
  8. $event->where = array($service->newWhere("Nagpur, India"));
  9. $event->content = $service->newContent("Some event content.");
  10.  
  11. // Create an object of When and set start and end datetime for the event
  12. $when = $service->newWhen();
  13. // Set start and end times in RFC3339 (http://www.ietf.org/rfc/rfc3339.txt)
  14. $when->startTime = "2010-07-08T16:30:00.000+05:30"; // 8th July 2010, 4:30 pm (+5:30 GMT)
  15. $when->endTime = "2010-07-08T17:30:00.000+05:30"; // 8th July 2010, 5:30 pm (+5:30 GMT)
  16. // Set the when attribute for the event
  17. $event->when = array($when);
  18.  
  19. // Create the event on google server
  20. $newEvent = $service->insertEvent($event);
  21. // URI of the new event which can be saved locally for later use
  22. $eventUri = $newEvent->id->text;

If you want the event to last the whole day then only set the date in startTime and skip endTime attribute of "when"

PHP:
  1. $when->startTime = "2010-07-08"; // 8th July 2010, Whole day event

Creating recurring events

Recurring events are created almost similar to non recurring events. The only difference is that instead of "where" attribute we would set "recurrence" attribute for the event. Recurrence attribute is a string pattern in iCalendar standard RFC2445. For more details please check out Google Data API Guide.

PHP:
  1. // This event will occur all day every Friday starting from 9th July 2010 until 8th Dec 2015
  2. $recurrence = "DTSTART;VALUE=DATE:20100709\r\n" .
  3.         "DTEND;VALUE=DATE:20100710\r\n" .
  4.         "RRULE:FREQ=WEEKLY;BYDAY=Fr;UNTIL=20151208\r\n";
  5.  
  6. $event->recurrence = $service->newRecurrence($recurrence);

Updating an event

For modifying an event, we first need to get its reference. This can be done using the event URI which we got after we created the event (see above). Once we get the event reference, we can pretty much change any attribute of it (as we did while creating an event) and then we have to save it.

PHP:
  1. // URI of the event which we got after creating it.
  2. $eventUri = "http://www.google.com/calendar/feeds/default/private/full/53608ibmrtnb57o7hqf8l1tsu4";
  3. // Get the event
  4. $event = $service->getCalendarEventEntry($eventUri);
  5. // Change the title
  6. $event->title = $service->newTitle("New Title!");
  7. // Save the event
  8. $event->save();

Deleting an event

Deleting an event is very simple. Just get the event reference (as we did for updating) and call the event's delete method.

PHP:
  1. // Get the event
  2. $event = $service->getCalendarEventEntry($eventUri);
  3. // Delete the event
  4. $event->delete();

So we conclude our three part series on Google Calendar API. As you saw it is very easy to access Calendar API with Zend's GData library which is simple yet powerful tool.

About Abbas Ali

Abbas Ali is a Mechanical Engineer by education. He turned to programming and took it as a profession just after finishing his studies. He is fascinated equally by both machines and computers. He leads the team of dynamic programmers at SANIsoft and works as a Technology Manager. He is also an active developer on the Coppermine Picture Gallery team.

21 Responses to [HowTo] Google Calendar API : PHP – Manage events (Part 3)

  1. Will September 19, 2010 at 1:43 am #

    just wanted to say thank you - this really helped me - so much easier to follow than the official google pages.

    really appreciate you taking the time.

    best wishes

  2. Alvaro October 7, 2010 at 2:31 am #

    very good, it is helpful your work, but it I have a problem when modify events
    I get this error:

    include (Zend / Gdata / Calendar / Extension / Title.php) [function.include]: failed to open stream: No such file or directory (line 82):

    I can do?
    thanks in advance

  3. Abbas Ali October 11, 2010 at 8:28 am #

    @Alvaro: Make sure you have downloaded the complete Zend package. It looks like some of the files are missing.

    Also make sure you have the correct path in $path

  4. Anonymous Coward October 13, 2010 at 1:51 pm #

    Is there a way to delete listfeeds (IE calendars) with the API aswell?

  5. Abbas Ali October 13, 2010 at 3:43 pm #

    I don't think calendars can be deleted with the API.

  6. Alvaro October 13, 2010 at 8:56 pm #

    thanks, In spite of the mistake everything works well,
    a new query is if you know of any documentation on adding events, but not in the default calendar. thanks again greetings.

  7. Abbas Ali October 15, 2010 at 11:30 am #

    @Alvaro: you can pass the URI of non-default calendar as a second argument to insertEvent() method. Something like this..

    $newEvent = $service->insertEvent($event, 'http://www.google.com/calendar/feeds/xyz@group.calendar.google.com/private/full');

    xyz@group.calendar.google.com in the above URI is the Calendar ID which can be found on Calendar Settings page in Google Calendar.

    • Anonymous Coward October 15, 2010 at 12:20 pm #

      @Abbas Ali: Speaking of which, is there a better way to get a calendars edit ID in ZF than str_replace('http://www.google.com/calendar/feeds/default/', '', $calendar->id)

      In latest ZF at time of writing $calendar->getEditLink() returns null or equivalent I believe

  8. Marco October 29, 2010 at 6:23 am #

    Thanks alot! Helped me work things out for sure :-)

  9. Zulqarnain December 27, 2010 at 4:37 pm #

    Thanks a lot! its really helpful and easy to learn.

  10. jamen May 26, 2011 at 12:47 am #

    how do i only update an event for today while not changing the other events if the event is recurring?

  11. Eddie June 21, 2011 at 1:00 am #

    AWESOME - AWESOME - AWESOME Tutorial. super easy to understand and to get working.

    Thank you for the time you took to put this together.

  12. Sonali Olwe November 10, 2011 at 12:34 pm #

    Hello Sir,

    I used this post and but getting this error message :

    Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Unable to Connect to ssl://www.google.com:443. Error #111: Connection refused' in /var/www/vhosts/nfs/DEV-NEW/vhq/Zend/Gdata/ClientLogin.php:141 Stack trace: #0 /var/www/vhosts/nfs/DEV-NEW/vhq/calendar/calendar.php(329): Zend_Gdata_ClientLogin::getHttpClient('sudhansun@smart...', 'smartdata', 'cl') #1 /var/www/vhosts/nfs/DEV-NEW/admin/RTH/admin.php(49): require_once('/var/www/vhosts...') #2 /var/www/vhosts/nfs/DEV-NEW/pagetemplates/RTH/vhq.php(36): require_once('/var/www/vhosts...') #3 /var/www/vhosts/nfs/DEV-NEW/vhq/admin.php(57): require_once('/var/www/vhosts...') #4 {main} thrown in /var/www/vhosts/nfs/DEV-NEW/vhq/Zend/Gdata/ClientLogin.php on line 141

    Can you please help me how it can be solved.

    Thanks & Regards,
    Sonali Olwe

    • Abbas Ali November 10, 2011 at 1:27 pm #

      Sonali,

      From the error message it looks like the port 443 is closed for outbound calls in your firewall. The error basically says that your connection request is refused by the server for some reason. It is more of a server issue than the code issue.

      Cheers.

      • Daniel Mcload December 6, 2011 at 8:32 pm #

        Do you have a still working copy of this code? Is it possible that Google does not support API V1 anymore?

        I got the following error: PHP Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Unable to Connect to ssl://www.google.com:443. Error #110:

  13. Remya November 19, 2011 at 2:05 pm #

    Hello Sir

    I want to know whether there is any possibility to get Multiple events in a day.Say i have 5 events in a day. i want to fetch those 5 events.
    i couldn't find solution for this. I have tried all options but i can see only 1 event /day is fetching using this API.

    Please help me to come out of this issue

    Thanks
    Remya.

  14. Teddy Aprilianto January 5, 2012 at 12:21 pm #

    Hi ,

    Can you give a sample how to make event public on non default calendar ?

    I've tried $newevent->visibility = "public"; and $newevent->setVisibility = "public";
    but doesn't work.

    Thanks
    Teddy

  15. Susan January 8, 2012 at 5:49 am #

    Thank you so much for this, very easy to follow and worked like a charm! Quick question... doesn't this make your Google name and password open to anyone who knows how to "view source" ??? How can we make that more secure?

Leave a Reply