Create a Google Calendar Event

I am trying to create a Google calendar event using the following code below, but I am getting an Event class 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()) { 

a new event is created here

  $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(); } 
+2
source share
2 answers

I had the same problem. Their documentation is very incomplete. Class names are incorrect 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 its code example. If you want it to return an object, you need to define it in a call to Google_Client , for example:

 $client = new Google_Client(array('use_objects' => true)); 
+9
source

I had this problem too, and it looks like everything has changed with the Google API.

Using sample code in docs I tried to do

 $calendar = new Calendar(); 

This generated an error for the Calendar class not found. It turns out that everything was renamed, and I had to do the following:

 $calendar = new Google_Service_Calendar_Calendar(); 

It looks like a naming convention to avoid conflicts, but it would be nice to update the documents. Therefore, to solve the OP problem today, he now solves his problem using:

 $event = new Google_Service_Calendar_Event(); 

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

+1
source

Source: https://habr.com/ru/post/1501231/


All Articles