1

Can anybody point me in the direction of examples of batch entry of events using the Google Calendar API.

The Google reference is https://developers.google.com/google-apps/calendar/batch and the example is:

POST /batch HTTP/1.1
Authorization: Bearer your_auth_token
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>

PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"

{
  "animalName": "sheep",
  "animalAge": "5"
  "peltColor": "green",
}

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>

GET /farm/v1/animals
If-None-Match: "etag/animals"

--batch_foobarbaz--

I've built a diary sync program which works well. It creates and populates a diary from our application. However the populating takes a long time, hence batch entry.

I want to use php and curl as the synch program was written in this way. I have used the PHP libraries in the past but don't want to use them for this.

Colin Martin
  • 41
  • 1
  • 3
  • Check this link https://code.google.com/p/google-api-php-client/source/browse/trunk/src/service/Google_BatchRequest.php?r=490 – SGC Nov 20 '14 at 22:40
  • Also, check this http://stackoverflow.com/questions/17840333/google-calendar-api-batch-request-php – SGC Nov 20 '14 at 22:48
  • Thanks for the answers. I don't really want to use the Google APIs Client Library for PHP this time as I've made a small and lean program submitting the requests using PHP curl. It works a faster and takes up less memory. I was hoping to submit the batch request through curl. The batch looks like a good alternative to submitting each event individually because when the submissions are kept to less than 5 per second it can take time. I'm also watching the calendar so each request causes a notification which seems wasteful. – Colin Martin Nov 21 '14 at 11:46
  • Make sure to respect the empty lines. The request is indeed sent to "/batch" endpoint (it's not calendar specific). – luc Nov 25 '14 at 02:27

1 Answers1

1

Here are my 2 cents:

I go through a list of events I scheduled in my LOCAL postgres database and that I synchronized with Google Calendar. I want to check if I modified them directly on Google Calendar (and not on my database).

Basically, in PHP, I wrote:

$batch = '';
$boundary = '--my_boundary';
$i = 0;
while ($event = pg_fetch_array($list_of_events)){
    $batch .= "\n" . $boundary . "\nContent-Type: application/http\nContent-ID: " . $event['meeting_id'] . "\n\nGET /calendar/v3/calendars/primary/events/" . $event['meeting_id'] . " \nIf-None-Match: \"" . $event['etag'] . "\"\n";
    $i ++ ;
    if ($i > 48){
        // we need to split the batch by packs of <50. Close this one and create the next one
        $batch .= "\n" . $boundary . "\n";
        $batches[] = $batch;
        $batch = '' ;
        $i=0;
    }
}
// close the current batch pack
if ($i > 0) $batches[] = $batch . "\n" . $boundary . "\n";

// send the packs. This can surely be optimized.
for ($i = 0 ; $i < count($batches) ; $i++) {
    $session = curl_init('https://www.googleapis.com/batch');
    curl_setopt($session, CURLOPT_POST, true);
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Host: www.googleapis.com', 'Content-Type: multipart/mixed; boundary=' . $boundary, 'Authorization:  Bearer ' . $token, 'Content-Length: ' . strlen( $batches[$i])));
    curl_setopt ($session, CURLOPT_POSTFIELDS,  $batches[$i]);
    curl_setopt($session, CURLOPT_HEADER, true);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_VERBOSE, true);
    curl_setopt($session, CURLINFO_HEADER_OUT, true);
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

    $resp = curl_exec($session);
    $api_response_info = curl_getinfo($session);
    $pack_of_answers = substr($resp, $api_response_info['header_size']);
    curl_close($session);

    // the pack of answers is in $pack_of_answers. You can split them thanks to a boundary, and deal with them at your convenience.
}
Charlie Echo
  • 87
  • 1
  • 5
  • this solution is not working for me. Getting the response as bad request. Can you show me a sample value for $event['etag'] – Jobz Dec 11 '17 at 07:27