$ Calendar未在Google Calendar API中定义

时间:2017-07-17 23:55:42

标签: php calendar google-calendar-api

我正在尝试使用Google的Calendar 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 Client Library。

其他人有这个问题吗?

以下是我的代码:

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

1 个答案:

答案 0 :(得分:1)

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

$service = new Google_Service_Calendar($client);

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