Google Calendar API批处理请求PHP [英] Google Calendar API batch request PHP

查看:95
本文介绍了Google Calendar API批处理请求PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 doesnt help me.

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.xml contains 2 -items. Thats all so far. But nothing happened.

我也尝试过

$batch->execute() 

但是那会抛出错误而没有消息.

But thats throws error without message.

我的问题:如何通过PHP将批处理发送到Google日历?

My question: How to send a Batch via PHP to Google Calendar ?

推荐答案

我正在使用最新版本的PHP的Google API客户端库(Google日历v.3).我使用批处理操作将讲师课程推送到Google日历.这是我为您提供的代码示例(multipleInsert函数).祝你好运!

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

}

这篇关于Google Calendar API批处理请求PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆