2012-10-31 119 views
2

正在尝试使用下面给出的以下代码创建谷歌日历事件,但我正在获取类未找到事件。如何创建一个新的事件。请帮助创建谷歌日历事件

<?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()) { 

我在这里创造新的事件

  $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(); 
} 

回答

9

我有完全相同的问题。他们的文件很不完整。他们的例子中的类名是错误的。这是我工作的代码:

$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返回一个数组,而不是一个对象就像在他们的示例代码。如果你希望它返回一个对象,你需要在你的Google_Client调用来定义它,就像这样:

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

你先生,我的一天。感谢这段伟大的代码。 – 2013-05-27 16:55:36

+0

一个问题: 您是否有类似的代码段列出所有事件? 再次感谢。 – 2013-05-27 16:55:53

+1

@JeanPaul对不起,没有示例代码,但使用listEvents函数返回一个事件列表。 https://developers.google.com/google-apps/calendar/v3/reference/events/list – Michael

1

我有这个问题太,似乎事情已经与谷歌的API又变了。

使用的文档我试图代码示例做

$calendar = new Calendar(); 

这投掷类日历的错误没有找到。原来一切都被改名,我不得不做以下几点:

$calendar = new Google_Service_Calendar_Calendar(); 

看起来像一个确定的命名约定,以避免冲突,但它本来不错,以更新文档。因此,要解决OP的问题,今天他现在使用解决他的问题:

$event = new Google_Service_Calendar_Event(); 

您可以浏览文件谷歌/服务/ Calendar.php,你应该看到与此命名约定命名有关的类。