Colin Martin
Colin Martin

Reputation: 41

Google Calendar API using PHP curl for batch

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:[email protected]>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:[email protected]>

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:[email protected]>

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.

Upvotes: 1

Views: 1783

Answers (1)

Charlie Echo
Charlie Echo

Reputation: 107

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

Upvotes: 1

Related Questions