php - 创建谷歌日历事件

标签 php google-calendar-api

我正在尝试使用下面给出的以下代码创建谷歌日历事件,但我得到类 Event not found 。如何创建新事件。请帮忙

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

最佳答案

我遇到了完全相同的问题。他们的文档非常不完整。他们的例子中的类名是错误的。这是我开始工作的代码:

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

关于php - 创建谷歌日历事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13152587/

相关文章:

php - PDO mysql 更新错误

calendar - Google Calendar API 从谷歌日历设置中获取工作时间数据

mysql - 用于更新谷歌日历的表单

google-calendar-api - 设置了错误 : No access, 刷新 token 或 API key

google-chrome - Google 通讯录生日未更新 Google 日历

authentication - 授权 Google 待办事项列表 (AuthToken, secid)

php - 远程 PHP 服务器中的 Android 应用内计费安全模块 : do I have to rewrite all the JAVA code

php - Laravel 重复延迟工作

javascript - Javascript 变量到 PHP int

php - 在 PHP 中创建两个相同的静态类