4

Deleting a Google Calendar with PHP

Posted October 13th, 2010 (Updated 29 Oct 2011) in PHP

I’ve recently started looking at the Google Calendar Data API for PHP for use in an upcoming project. It uses Zend Framework which has a pretty nice library complete with documentation here allowing for just about every operation I required…except deleting calendars (which is a bit strange, considering you’re able to create them with the API). This seems to be a limitation with version 1.0 of the API. Version 2 allows for deletion of calendars however there’s no documentated PHP implementation yet. After a long night of researching and hair pulling I’ve finally managed to come up with a working script provided below.

//Info required to authenticate
$URL = "https://www.google.com/accounts/ClientLogin";
$POST = http_build_query(array(
 'Email' => '[email protected]',
 'Passwd' => 'yourpassword',
 'source' => 'yourappname',
 'service' => 'cl'
));
 
$ch = curl_init( $URL );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // RETURN THE CONTENTS OF THE CALL
$response = curl_exec($ch); //returns SID=<sid code>nLSID=<lsid code>nAuth=<auth code> or ERROR=<message>
 
if ( curl_errno($ch) )
 die( 'Error contacting server' );
 
//Successful auth results in http code 200
if ( curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200 )
 die( 'Failed to authenticate' );
 
//Extract auth code - Authorization: GoogleLogin auth=yourAuthToken
$auth_code = substr($response, strpos($response, 'Auth=')+5);
 
//We're done here
curl_close($ch);
 
//Now delete the actual calendar entry
//You'll need your calendars edit URL
$URL = "https://www.google.com/calendar/feeds/default/owncalendars/full/";
$ch = curl_init( $URL );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=$auth_code"));
 
//Successful auth results in http code 200
if ( curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200 )
 die( 'Failed to delete calendar' );
 
curl_close($ch);

Hopefully this’ll help you out there in the same boat as me.

EDIT: Things just got easier – here’s how to delete calendars with Zend Framework!

// Parameters for ClientAuth authentication
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
// Create an authenticated HTTP client
$client = Zend_Gdata_ClientLogin::getHttpClient('[email protected]', 'yourpassword', $service);
// Create an instance of the Calendar service
$service = new Zend_Gdata_Calendar($client);
 
$response = $service->performHttpRequest(
	'DELETE',
	"https://www.google.com/calendar/feeds/default/owncalendars/full/"
);

To find the URL for your calendar go to http://www.google.com/calendar, click Settings->Calendar Settings in the menu at the top right, Calendars tab, click your calendar name and under the Embed This Calendar section you’ll see some iframe code containing the calendars URL. It should look something like

weoihweoriu237dnweij02hei%40group.calendar.google.com
  • zerodtkjoe

    Thanks for the info

  • seanlopez

    Good article

  • bart

    Works great! Thank you!

  • Joselito

    I tried your code however I got a fatal error. I am using the zend library .

    // Parameters for ClientAuth authentication
    $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    // Create an authenticated HTTP client
    $client = Zend_Gdata_ClientLogin::getHttpClient(‘[email protected]’, ‘#######’, $service);
    // Create an instance of the Calendar service
    $service = new Zend_Gdata_Calendar($client);

    $response = $service->performHttpRequest(
    ‘DELETE’,
    ‘https://www.google.com/calendar/feeds/58a354dktnq57kcrlahvejj3qo%40group.calendar.google.com/private/full/’
    );
    Fatal error: Uncaught exception ‘Zend_Gdata_App_HttpException’ with
    message ‘Expected response code 200, got 400
    Invalid request URI’ in
    /var/www/vhosts/apptrsvp.com/Zend/Gdata/App.php:714
    Stack trace:
    #0 /var/www/vhosts/apptrsvp.com/Zend/Gdata.php(219):
    Zend_Gdata_App->performHttpRequest(‘DELETE’, ‘https://www.goo…’,
    Array, NULL, NULL, NULL)
    #1 /var/www/vhosts/apptrsvp.com/sample-deletecal.php(26):
    Zend_Gdata->performHttpRequest(‘DELETE’, ‘https://www.goo…’)
    #2 {main}
    thrown in /var/www/vhosts/apptrsvp.com/Zend/Gdata/App.php on line 714