Google Api v1/v2 is dead, move on

Please note the Google PHP Api changes regularly and none of this code is future proof as the google api client is still in beta

Recently I undertook a php application that was using a ‘zend’ google calendar api. Well as of the 17th of November 2014 the google api v1/2 (used internally by zend) shut down. I used this as a learning opportunity to impl google api v3.

So here is the code that I have come up with that has worked pretty well for me. It is very similar to zend.

First we setup our google client from the client implementation on Google Api Php Client, you will need the client in your application somewhere and will need to require the AutoLoad.php in order to use it. (Not shown below)

Code it out

$user = '';
$gclient = new Google_Client();
$gclient->setApplicationName("My Apps Name");

if (isset($_SESSION[$user.'_service_token'])) {
$key = file_get_contents($privateKeyLocation);
$auth = new Google_Auth_AssertionCredentials(
	$config->google_email_address, //this is my service account email **crazy_string_here**
	array(''),//Google calendar permissions
	$key //contents of my .p12 file i downloaded from
$auth->sub = $user;//This is saying our service account will be acting as the sub user
if ($gclient->getAuth()->isAccessTokenExpired()) {
$_SESSION[$user.'_service_token'] = $gclient->getAccessToken();

At this point we have setup all of our google_client code so we can add our implemented “add calendar event”. First Instantiate the calendar service.

$cal_service = new Google_Service_Calendar($gclient);

Now we need to set up an event, give it a “summary”, location, and a start and end datetime in format



$event = new Google_Service_Calendar_Event();
	$event->setSummary("Event Title");
	$event->setLocation("123 Fake St. Dover, ME 41426");//Address City, State Zip
	$event->setDescription("You have a new event on your calendar!");
	$endTime = clone $aDateTimeObject;
	$endTime->add(new DateInterval('PT1H'));//1 hour calendar event
	$start = new Google_Service_Calendar_EventDateTime();
	$end = new Google_Service_Calendar_EventDateTime();
	$createdEvent = $cal_service->events->insert($user, $event);

At this point we have a calendar event returned to us and can use


to get the event ID if we want to store it so it can be updated/deleted later in similar fashion like so:

$cal_service->events->delete($user, $createdEvent->id);

Hopefully this was enlightening for you and with all the recent changes (and shutdowns) of the google api this can help you transition your application more quickly to the new version.