Google Calendar List Class

    Current Version: 1.1.0
    Price: FREE

    Google Calendars are incredibly useful for sharing and managing events — both through directly sharing calendars with friends and the ability to quickly and easily embed them in websites. Another lesser know feature is the ability to retrieve Google calendar events for use in custom programs or webpages, grabbing as much or as little information about an event as you would like. This framework provides easy access to the most common information people are looking for about events; The events name, start date & time, end date & time, and description. It also returns the event id used by google, which may prove useful as an index in custom applications. For complete usage information see below. You can also download a functional example.


                 

    Dependencies
    Zend Gdata PHP Client Library
    PHP 5.1+


    The Class
    Start by creating a directory to work in. For the sake of this example name it ‘googcalget’. Download and unpack the Zend Gdata PHP Client Library. Upload the directory ‘Zend’ into the ‘googcalget’ directory. Create a file named ‘cls.googcalget.php’. Copy and paste the code below into the file you’ve just created.

    //#######################################################################\\
    //# Author: Christopher Schiffner                                       #\\
    //# Filename: cls.googcalget.php                                        #\\
    //# Copyright: Christopher Schiffner, All Rights Reserved               #\\
    //# Description: Google Calendar Event Listing Class                    #\\
    //# Version: 1.1.0                                                      #\\
    //# Details: Retrieves events from a Google calendar                    #\\
    //#                                                                     #\\
    //# License: the new BSD license                                        #\\
    //#######################################################################\\
    
    class clsGoogCalGet
    {
    
    	//setup our vars
    	private $user="";
    	private $pass="";
    	private $service="";
    	private $client="";
    	private $calid="";
    	private $startDate="";
    	private $endDate="";
    	private $futureEvents="";
    	private $maxResults="";
    
    	//prepare Zend Gdata
    	function __construct($user, $pass) {
    		require_once 'Zend/Loader.php';
    		Zend_Loader::loadClass('Zend_Gdata');
    		Zend_Loader::loadClass('Zend_Gdata_AuthSub');
    		Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    		Zend_Loader::loadClass('Zend_Gdata_Calendar');
    
    		$this->user = $user;
    		$this->pass = $pass;
    		$this->service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    		$this->client = Zend_Gdata_ClientLogin::getHttpClient($this->user,$this->pass,$this->service);
    	}
    
    	//set calendar id
    	public function setCalID($calid){
    		$this->calid=$calid;
    	}
    
    	//set start date
            public function setStartDate($startDate){
                    $this->startDate=$startDate;
            }
    
    	//set end date
            public function setEndDate($endDate){
                    $this->endDate=$endDate;
            }
    
    	//set max results
            public function setMaxResults($maxResults){
                    $this->maxResults=$maxResults;
            }
    
    	//set futuree events
            public function setFutureEvents($futureEvents){
                    $this->futureEvents=$futureEvents;
            }
    
    	//run private function getCalEvents and retunr results to user
            public function getEvents(){
    		$calOutput=$this->getCalEvents();
    
                    if(is_array($calOutput)){
                            $calOutput=array_reverse($calOutput);
                    }else{
                            $calOutput="No Events";
                    }
    
    		return $calOutput;
    	}
    
    	private function getCalEvents()
    	{
    	  $gdataCal = new Zend_Gdata_Calendar($this->client);
    	  $query = $gdataCal->newEventQuery();
    	  $query->setUser($this->calid);
    	  $query->setVisibility('public');
    	  $query->setProjection('full');
    	  $query->setOrderby('starttime');
    
    	  //only apply filters for parameters the user has entered
    	  if($this->startDate!=""){
    		  $query->setStartMin($this->startDate);
    	  }
    
    	  if($this->endDate!=""){
    		  $query->setStartMax($this->endDate);
    	  }
    
    	  if($this->maxResults!=""){
    		  $query->setMaxResults($this->maxResults);
    	  }
    
    	  if($this->futureEvents){
    		  $query->setfutureevents($this->futureEvents);
    	  }
    
    	  $eventFeed = $gdataCal->getCalendarEventFeed($query);
    
    	  foreach ($eventFeed as $event) {
    	    foreach ($event->when as $when){
    
    		//split start date from string and convert utc to 12 hour am/pm
    		//index 0 = date, index 1 = time
    		$aryDateStart=split("T", $when->startTime);
    		if($aryDateStart[1]==""){
    			$aryDateStart[1]="ALLDAY";
    		}else{
    			$aryDateStart[1]=date('g:i a', strtotime($when->startTime));
    		}
    
    		//split end date from string and convert utc to 12 hour am/pm
    		//index 0 = date, index 1 = time
    	        $aryDateEnd=split("T", $when->endTime);
    	        if($aryDateEnd[1]==""){
    	                $aryDateEnd[1]="ALLDAY";
    	        }else{
    	                $aryDateEnd[1]=date('g:i a', strtotime($when->endTime));
    	        }
    
    		$aryID=split("full/", $event->id->text);
    
    		$aryCalendarEvents[]=array($aryDateStart[0], $aryDateStart[1], $aryDateEnd[0], $aryDateEnd[1], $event->title->text, $event->content->text, $aryID[1], $event->where[0]);
    	    }
    	  }
    
    	  return $aryCalendarEvents;
    	}
    
    }
    



    Example Usage
    To use the class you’ve just created above, create a file named ‘index.php’ (or any other name you’d prefer) in the ‘googcalget’ directory. Copy and paste the code below into the file you’ve just created. Replace the information in brackets with your google account user/pass/calendar id. Please note the start date and end dates and modify as necessary. It is not necessry to include any or all of the listing parameters.

    	include_once 'cls.googcalget.php'; //path to php file for Panda Google Calendar Get class
    
    	$clsGoogCalGet = new clsGoogCalGet('<GOOGLE ACCOUNT EMAIL>','<GOOGLE ACCOUNT PASSWORD>'); // instantiates class -- sets username and pass for google calendar
    	$clsGoogCalGet->setCalID('<GOOGLE CALENDAR ID>'); // google calendar id
    	$todaysDate=date('Y-m-d'.' 00:00:00'); // sets todays date
    	$clsGoogCalGet->setStartDate(date('Y-m-d\TH:i:s', strtotime($todaysDate.'+1 days')).'.000Z'); // sets first date to list events for
    	$clsGoogCalGet->setEndDate(date('Y-m-d\TH:i:s', strtotime($todaysDate.'+6 days 23 hours 59 minutes 59 seconds')).'.000Z'); // sets last date to list events for
    	$clsGoogCalGet->setMaxResults(20); // overrides end date
    	$clsGoogCalGet->setFutureEvents(true); //sets whether to list only future events -- values: true, false
    
    	/*
    	clsGoogCalGet returned array structure
            [0]: start date
            [1]: start time
            [2]: end date
            [3]: end time
            [4]: event title
            [5]: event details
            [6]: event id
            [7]: event location
    	*/
            $aryCalendarEvents=$clsGoogCalGet->getEvents();
    	foreach($aryCalendarEvents as $event){
    		echo $event[0]." ".$event[1]." ".$event[2]." ".$event[3]." ".$event[4]." ".$event[5]." ".$event[6]." ".$event[7]."<br/>";
    	}