php - Google 日历 API 批量请求 PHP

标签 php google-calendar-api google-api-php-client google-api-client

我正在尝试通过批量请求向 Google 日历发送一堆事件。 但我不知道该怎么做。 https://developers.google.com/google-apps/calendar/batch对我没有帮助。

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';

$client = new Google_Client();
$client->setUseBatch(true);
$batch = new Google_BatchRequest();

$uri = 'http://www.google.com/calendar/feeds/default/private/full/batch';
$batchContent = file_get_contents('xxxxx/google-api-php-client/batch.xml');    

$batch->add($batchContent);

batch.xml 包含 2 个项目。 到目前为止,这就是全部。但什么也没发生。

我也尝试过

$batch->execute() 

但是这会引发错误而没有消息。

我的问题:如何通过 PHP 批量发送至 Google 日历?

最佳答案

我正在使用最新版本的 PHP 版 Google API 客户端库 (Google Calendar v.3)。我使用批量操作将讲师类(class)推送到 Google 日历。这是我为您编写的代码示例(multipleInsert 函数)。祝你好运!

<?php
require_once(APPPATH . 'libraries/Google/Client.php');
require_once(APPPATH . 'libraries/Google/Http/Batch.php');
require_once(APPPATH . 'libraries/Google/Service/Calendar.php');

class My_google_calendar
{
  ...

  /** Add single Event for Student */
  function addEvent($lesson, $instructor, $return_request = false, $enable_attendees = false)
  {
    $calendar = $this->getGoogleCalendar(); // get calendar service variable

    $lesson_from = date(DATE_RFC3339, $lesson->from);
    $lesson_to = date(DATE_RFC3339, $lesson->from + $lesson->duration); 

    $event = new Google_Service_Calendar_Event();
    $event->setSummary('Lesson with student: '$lesson->student_full_name);

    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDateTime($lesson_from); 
    $start->setTimeZone($this->getGoogleCalendarTimeZone());
    $event->setStart($start);

    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDateTime($lesson_to);
    $end->setTimeZone($this->getGoogleCalendarTimeZone());
    $event->setEnd($end);
    $event->setColorId(4);

    $description = "...";
    $event->setDescription($description);

    if (isset($student->email) && $enable_attendees) {
      $attendee1 = new Google_Service_Calendar_EventAttendee();
      $attendee1->setResponseStatus('needsAction');
      $attendee1->setEmail($student->email);
      $attendees = array($attendee1);
      $event->setAttendees($attendees);
    }

    $createdEvent = $this->calendar->events->insert($this->calendar_id, $event, array('fields' => 'id'));
    return $return_request ? $createdEvent : $createdEvent->getId();
  }

  /** Push group of events to the Calendar */
  function multipleInsert ($lessons, $instructor)
  {
    $this->use_batch = true;
    $this->client->setUseBatch($this->use_batch);
    $batch = new Google_Http_Batch($this->client);
    $optParams = array('fields' => 'status,updates');

    foreach($lessons as $time => $lesson) {      
        $lesson = array_shift($group['lessons']);
        $req = $this->addEvent($lesson, $instructor, true);
        $batch->add($req, $time);
      }
    }
    $results = $batch->execute();

    return $results;
  }

}

关于php - Google 日历 API 批量请求 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17840333/

相关文章:

php - codeigniter 新闻部分教程不起作用

javascript - php echo 中的隐藏值

iframe - 除非登录到 Google 帐户,否则嵌入式 Google 日历不会显示

Python Google Calendar API v3 在列出事件项时不返回 "summary"字段

iphone - Google 日历移动版 - 嵌入

google-drive-api - 如何使用 PHP API 更新 Google 电子表格单元格

php - 通过 google php client api v3 将大视频发布到 youtube

php - 调试MYSQL语法错误

更新查询后 PHP header 重定向不起作用

google-api-php-client - 如何通过 Google Sheets API 更新多个单元格?