'Batch request Google Calendar php API

I'm working on a google Calendar sync with my application. I'm using the latest google-api-php-client

Now I want to update all my event, so i want to use the batch operation. The example code of the php client api is:

$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();

So when I "translate" it to the calendar API, I become the following code: $client = new Google_Client(); $this->service = new 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();

But when I run this code, I get this error:

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

And I do not think that "$plus->people->get('')" is a HttpRequest.

Does anybody know what I do wrong, or what method / object I should use to add in the batch? Or what the correct use of the batch operation for the calendar is?

Thanks in advance!



Solution 1:[1]

I had the same problem while working with inserts to the MirrorService api, specifically with timeline items. What is happening is that the the Google_ServiceRequest object is seeing that you've set the useBatch flag on the client and is actually returning returning Google_HttpRequest object before executing the call to Google but the insert statement in the calendar service doesn't properly handle it as such and ends up returning the calendar event object instead.

It also looks like your params to batch->add are backwards. Should be:

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

Here is my modification to the insert method (you'll need to do this in the calendar service with the proper object input to the method). Just a few lines to make it check what class is coming back from the ServiceRequest class:

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

Solution 2:[2]

you can use this code to insert events in batch:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Flack
Solution 2 Jhkcia