【问题标题】:Adding events in bulk to Google Calendar API with PHP使用 PHP 将事件批量添加到 Google Calendar API
【发布时间】:2014-04-28 20:00:29
【问题描述】:

使用 PHP 一次将可能 10,000 到 20,000 个事件添加到 Google 日历的最佳方法是什么?

如果我尝试单独添加它们,我会在每日最大值之前收到“超出限制”错误。

我也一直在研究导入 ICAL/ICS 文件,但除了日历订阅之外,我似乎也找不到此功能。日历订阅很棒,但是当事件发生变化时,我需要在我的日历中进行几乎“实时”的更新,而且日历订阅何时更新尚不清楚,这可能需要从 ICS 文件的初始更改开始最多一天的时间。

【问题讨论】:

    标签: php google-calendar-api batch-processing icalendar


    【解决方案1】:

    需要使用批量请求。批处理请求由多个 API 调用组合成一个 HTTP 请求组成。批处理是一个已知的功能请求,并且正在积极处理。阅读以下关于它的文章:https://developers.google.com/google-apps/calendar/batch

    查看我的 multipleInsert 函数,例如:

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

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多