Creating Google Calendar Events with PHP

Many small businesses are now using the extensive Google Apps for Business framework to manage many of their daily activities. However due to the increase of Cloud Applications being programmed as well to assist this businesses, sometimes they have to be importing their online application information into their Google Apps. With the available API’s however this has not to be necessarily the case.

Using the new Google Calendar API and the following PHP code you can easily create events dynamically when they are generated in your cloud business application. Please be aware than in order to connect through their API you will need to have Zend installed on your server.

<?php
// Load required libraries
$path = 'Zend/Gdata';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');

class google {
	public $client;
	public $service;

	function createCalendarEvent( ) {
		// Authentication with Google
		$user = 'user@gmail.com';
		$pass = 'userPass';
        // Load required libraries
		 // predefined service name for calendar
		$serviceName = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
		$this->client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $serviceName);
		$this->service = new Zend_Gdata_Calendar( $this->client );
		// get time
        $this->eventStart = "07:00:00";
        $this->eventEnd = "08:30:00";
		// Create a new event to add required parameters
		$event = $this->service->newEventEntry();
		// Create a new title for your calendar event
		$event->title = $this->service->newTitle( 'My First Google API Interaction' );
		// Set a location for your event
		$eventAddress = '123 Test St, Chula Vista';
		$event->where = array( $this->service->newWhere( $eventAddress  ) );
		// You can also add aditional information to your appoitnment
		$event->content = $this->service->newContent( 'Extra event information');
		$when = $this->service->newWhen();
		// Set start and end times
		$eventInfo = '2008-09-08';
		// 2008-09-08T22:47:31-07:00 (For Pacific Times)
		$when->startTime = $eventInfo . "T" . $this->eventStart . "-07:00";
		$when->endTime = $eventInfo . "T" . $this->eventEnd . "-07:00";
		// Set the when attribute for the event
		$event->when = array($when);
		// Create the event on google server
		$newEvent = $this->service->insertEvent($event);
		// URI of the new event which can be saved locally for later use
		$eventUri = $newEvent->id->text;
	}
}

 

Leave a Reply

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