Google Calendar API batch request PHP

前端 未结 1 1024
臣服心动
臣服心动 2020-12-18 14:33

i am trying to send a bunch of events via an Batch Request to Google Calendar. But i cant figur out how to do. https://developers.google.com/google-apps/calendar/batch doesn

相关标签:
1条回答
  • 2020-12-18 15:15

    I'm using the latest version of Google APIs Client Library for PHP (Google Calendar v.3). I use batch operations for pushing instructor lessons to Google Calendar. Here is my example of code for you (multipleInsert function). Good luck!

    <?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;
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题