4

Deleting a Google Calendar with PHP

Posted (Updated ) 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' => 'youremail@gmail.com',
 '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('username@gmail.com', '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