批量请求Google Calendar php API


Batch request Google Calendar php API

我正在与我的应用程序进行谷歌日历同步。我正在使用最新的谷歌api php客户端

现在我想更新我的所有事件,所以我想使用批处理操作。php客户端api的示例代码是:

$client = new Google_Client();
$plus = new Google_PlusService($client);
$client->setUseBatch(true);
$batch = new Google_BatchRequest();
$batch->add($plus->people->get(''), 'key1');
$batch->add($plus->people->get('me'), 'key2');
$result = $batch->execute();

因此,当我将其"翻译"到日历API时,我变成了以下代码:$client=新的Google_client();$this->service=新的Google_CalendarService($client);

$client->setUseBatch(true);
// Make new batch and fill it with 2 events
$batch = new Google_BatchRequest();
$gEvent1 = new Google_event();
$gEvent1->setSummary("Event 1");
$gEvent2 = new Google_event();
$gEvent2->setSummary("Event 2");
$batch->add( $this->service->events->insert('primary', $gEvent1));
$batch->add( $this->service->events->insert('primary', $gEvent2));
$result = $batch->execute();

但是当我运行这个代码时,我得到了这个错误:

Catchable fatal error: Argument 1 passed to Google_BatchRequest::add() 
   must be an instance of Google_HttpRequest, instance of Google_Event given

我不认为"$plus->people->get('')"是HttpRequest。

有人知道我做错了什么吗,或者我应该使用什么方法/对象来添加到批中吗?或者,日历的批处理操作的正确用途是什么?

提前感谢!

我在处理MirrorServiceapi的插入时遇到了同样的问题,特别是在处理时间线项目时。发生的情况是,Google_ServiceRequest对象看到您已经在客户端上设置了useBatch标志,并且实际上在执行对Google的调用之前返回了Google_HttpRequest对象,但日历服务中的insert语句没有正确处理它,最终返回了日历事件对象。

看起来您要批量->添加的参数也是向后的。应为:

$batch->add( $this->service->events->insert($gEvent1, 'primary'));

以下是我对insert方法的修改(您需要在日历服务中使用该方法的正确对象输入来完成此操作)。只需几行就可以检查从ServiceRequest类返回的类:

public function insert(google_TimelineItem $postBody, $optParams = array()) {
  $params = array('postBody' => $postBody);
  $params = array_merge($params, $optParams);
  $data = $this->__call('insert', array($params));
  if ($this->useObjects()) {
    if(get_class($data) == 'Google_HttpRequest'){
        return $data;
    }else{
        return new google_TimelineItem($data);
    }
  } else {
    return $data;
  }
}

您可以使用此代码在批处理中插入事件:

public function addEventInBatch($accessToken, $calendarId, array $events)
{
    $client = new Google_Client();
    $client->setAccessToken($accessToken);
    $client->setUseBatch(true);
    $service = new Google_Service_Calendar($client);
    $batch = $service->createBatch();
    collect($events)->each(fn ($event) => $batch->add($service->events->insert($calendarId, $event)));
    return $batch->execute();
}