$service 未在 Google 日历 API 中定义

$service not defined in Google Calendar API

我正在尝试使用 Google 的日历 API 创建(插入)一个新事件。我保持简单并使用 quickstart.php 进行测试。最初的 quickstart.php 代码有效(获取日历事件),所以我知道我可以连接。

但是,当我尝试使用 events.insert() 时,出现以下错误:

PHP Notice:  Undefined variable: service in /webroot/Website/TestWebsite/root/inc/Calendar/quickstart.php on line 43

很明显 $service 没有定义,但我不确定我遗漏了什么。

我使用 Composer 安装了 Google 客户端库。

还有其他人有这个问题吗?

下面是我的代码:

<?php
require_once __DIR__ . '/vendor/autoload.php';


define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/calendar-php-
quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-php-quickstart.json
define('SCOPES', implode(' ', array(
Google_Service_Calendar::CALENDAR)
));

$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Google I/O 2015',
  'location' => '800 Howard St., San Francisco, CA 94103',
  'description' => 'A chance to hear more about Google\'s developer 
products.',
  'start' => array(
    'dateTime' => '2015-05-28T09:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'end' => array(
    'dateTime' => '2015-05-28T17:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2'
  ),
  'attendees' => array(
    array('email' => 'lpage@example.com'),
    array('email' => 'sbrin@example.com'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$calendarId = 'foo_calendar_ID';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);

当您使用 PHP 快速入门时,$service 变量可以正常工作,因为它是在此行中定义的

$service = new Google_Service_Calendar($client);

现在,您修改了代码,它开始出现错误。查看您的代码,您似乎已经删除了它。现在你知道是什么原因造成的了:)