2

Google 캘린더에 일괄 요청을 통해 여러 가지 이벤트를 보내려고합니다. 하지만 어떻게해야할지 모릅니다. https://developers.google.com/google-apps/calendar/batch 나를 도와주지 않습니다.Google 캘린더 API 일괄 요청 PHP

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 -items가 포함되어 있습니다. 그 모두가 지금까지 있습니다. 그러나 아무 일도 일어나지 않았습니다.

또한

$batch->execute() 

을 시도하지만 메시지없이 오류를 그게 전부가 발생했다.

질문 : PHP를 통해 Google 캘린더로 일괄 전송하는 방법은 무엇입니까?

답변

3

PHP 용 최신 Google API 클라이언트 라이브러리 (Google 캘린더 v.3)를 사용하고 있습니다. 강좌 레슨을 Google 캘린더에 푸시하는 데 배치 작업을 사용합니다. 여기에 당신을위한 코드 예제 (multipleInsert function)가있다. 행운을 빕니다!

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

이것은 완벽한 예입니다. 나 한테 도와 줘서 고마워, @ Natali! –