2

am trying to create google calendar event using the following code given below, but am getting class Event not found . How to create a new event. please help

<?php
         require_once '../../src/Google_Client.php';
         require_once '../../src/contrib/Google_CalendarService.php';
         session_start();

         $client = new Google_Client();
         $client->setApplicationName("Google Calendar PHP Starter Application");

         $client->setClientId('');
         $client->setClientSecret('');
         $client->setRedirectUri('simple.php');
         $client->setDeveloperKey('insert_your_developer_key');
         $cal = new Google_CalendarService($client);
         if (isset($_GET['logout'])) {
           unset($_SESSION['token']);
         }

         if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $_SESSION['token'] = $client->getAccessToken();
            header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
        }

        if (isset($_SESSION['token'])) {
           $client->setAccessToken($_SESSION['token']);
        }

        $authUrl = $client->createAuthUrl();
if (!$client->getAccessToken()) {

here am creating the new event

          $event = new Event();
          $event->setSummary("test title");
          $event->setLocation("test location");
          $start = new EventDateTime();
          $start->setDateTime('04-03-2012 09:25:00:000 -05:00');
          $event->setStart($start);
          $end = new EventDateTime();
          $end->setDateTime('04-03-2012 10:25:00:000 -05:00');

          $createdEvent = $cal->events->insert('primary', $event);

          echo $createdEvent->getId();
}
kelin
  • 11,323
  • 6
  • 67
  • 104
Nyfer
  • 163
  • 2
  • 5
  • 15

2 Answers2

11

I had the exact same problem. Their documentation very incomplete. The class names are wrong in their example. Here is the code I got to work:

$event = new Google_Event();
$event->setSummary('Halloween');
$event->setLocation('The Neighbourhood');
$start = new Google_EventDateTime();
$start->setDateTime('2012-10-31T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-10-31T10:25:00.000-05:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert('[calendar id]', $event); //Returns array not an object

echo $createdEvent->id;

$cal->events->insert returns an array, not an object like in their example code. If you want it to return an object you need to define it in your Google_Client call, like so:

$client = new Google_Client(array('use_objects' => true));
Michael
  • 763
  • 3
  • 5
  • One question: Do you have a similar piece of code for listing all the events? Thanks again. –  May 27 '13 at 16:55
  • 1
    @JeanPaul Sorry no example code, but use the listEvents function to return an event list. https://developers.google.com/google-apps/calendar/v3/reference/events/list – Michael May 29 '13 at 15:20
1

I had this problem too, and it seems things have changed with Google's API again.

Using the code example in the docs I was trying do

$calendar = new Calendar();

This threw an error for class Calendar not found. Turns out everything was renamed and I had to do the following:

$calendar = new Google_Service_Calendar_Calendar();

Seems like an ok naming convention to avoid conflicts, but it would have been nice to update the docs. So to solve OP's problem today he would now solve his issue using:

$event = new Google_Service_Calendar_Event();

You can look through the file Google/Service/Calendar.php and you should see all the relevant classes named with this naming convention.

Syntax Error
  • 4,475
  • 2
  • 22
  • 33