jump to navigation

Integrating PHP Applications with Google’s Data Protocol February 27, 2012

Posted by Tournas Dimitrios in PHP.

The Google Data Protocol is a  programmatic interface (API) to some of Google’s online services  for reading  , writing , and modifying information on the web (this protocol currently supports two primary modes of access AtomPub and JSON) . A developer can access these services with a plethora of  libraries that support almost all programming platforms  (Java , .NET, PHP, Python , JS , Obj-C)  .  For example , Zend Framework’s  Zend_Gdata component provides the necessary PHP-Classes to access many of Google’s Data Protocol services  (  lists of all of the Google API’s that use the Google Data Protocol ). An older article of this Blog  “Simple Use of YouTube’s API with Zend-Framework’s VideoEntry Component ” demonstrated the use of Zend’s  Zend_Gdata component , this article will present a practical example how to access another interesting service , the “Google Calendar API” .
Google’s calendar API works by accepting HTTP requests containing one or more XML-encoded input arguments and returning XML-encoded responses that can be parsed in any XML-aware client  . To see an example , log into your Google Calendar account , navigate to your calendar settings , and find the link for your calendar’s private address URL (admin-panel->settings->calendars->select-a-calendar->private-adress) . This URL , which you should hold secret — unless you want other people to see all the events on this calendar — , provides read-only access to your calendar’s feed without first requiring authorization . Pop this URL into your Web browser (it sends a GET request for the feed ) . The result is a raw XML-Feed that is difficult to read , unless a script  extracts relevant fragments of data from it and format it into a Web page . Different approaches could be applied to achieve this goal , let see a couple practical examples :

Google also provides the code to embed  the calendar into our web-page (non-interactive mode) via an IFrame  panel (admin-panel->settings->calendars->select-a-calendar->Embed-This-Calendar)  .

Parsing the XML-Feed with SimpleXML : see a live demo 

<!DOCTYPE html 
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
    <title>Listing calendar contents</title>
    body {
      font-family: Verdana;      
    li {
      border-bottom: solid black 1px;      
      margin: 10px; 
      padding: 2px; 
      width: auto;
      padding-bottom: 20px;
    h2 {
      color: red; 
      text-decoration: none;  
    span.attr {
      font-weight: bolder;  

//Fake parameters use your own
$userid = 'tournasdimitrios%40gmail.com';
$authentication = '68a2ea4dc0085d0c373e957f267e0411' ;  
    // build feed URL
 $feedURL = "http://www.google.com/calendar/feeds/$userid/private-$authentication/basic";    
    // read feed into SimpleXML object
    $sxml = simplexml_load_file($feedURL) ;    
    // get number of events
    $counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
    $total = $counts->totalResults; 
    <h1><?php echo $sxml->title; ?></h1>
    <?php echo $total; ?> event(s) found.
    // iterate over entries in category
    // print each entry's details
    foreach ($sxml->entry as $entry) {
      $title = stripslashes($entry->title);
      $summary = stripslashes($entry->summary);
	  $content = stripslashes($entry->content);
	  $id = stripslashes($entry->published);
      echo "<li>\n";
      echo "<h2>$title</h2>\n";
	  echo "$content<br>";
      //echo "$summary <br/>\n";
	  echo "<b>Published</b>  : $id";
      echo "</li>\n";

The result in the browser is as follows :

You can also  interact  with Google’s Calendar API through  Zend’s Framework Zend_Gdata component . My preferred method is to install  the whole library into my server (it’s 30Mbyte )  , if you have a good reason , just download and install only the Zend_Gdata component-part on your server . In this examples I am going to use a private Google-Calendar , this demands credentials for authentication which can be retrieved — once a Calendar is created — from the administration panel of the Google-Calendar site ( settings -> calendars -> Select-a-private-calendar -> Calendar-Access-section ). It looks like – Calendar ID: c4dq8i055u4k16aib98pr4gr5s@group.calendar.google.com

Querying the Calendar with Zend’s Zend_Gdata component : see a live demo 

<!DOCTYPE html 
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
    <title>Listing calendar contents</title>
    body {
      font-family: Verdana; 
	  background-color: beige;
	  margin-left : 25px;
	  margin-right: 25px; 
	   h1 {
	   color: green ;
	   text-align : center ;
	   h2 {
      color: red; 
      text-decoration: none; 
      margin-bottom: 2px;
	b {
	color : blue;

require_once 'Zend/Loader.php'; 
//Fake credentials : Use your own Gmail-account's credentials  
$user = 'tournasdimitrios@gmail.com' ;
$pass = 'vcbbcbcxxttere[=00p4%*mSF' ;
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; 
	$client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service);	

// Fake parameters use your own
$calendar_user = 'ipad3abo8ab97r6hi197v8rft8@group.calendar.google.com';
$calendar_visibility = 'private-123bbf548cfa7e5039495860452f22a0';
$start_date = '2012-02-27';
$end_date = '2012-03-28';
// build query
$gdataCal = new Zend_Gdata_Calendar($client); 
$query = $gdataCal->newEventQuery(); 
// execute and get results
$event_list = $gdataCal->getCalendarEventFeed($query);
echo " <h1> My sceduled meetings </h1>" ;
$count = 0 ;
foreach ($event_list as $event){
$count ++ ;
echo "Meetings found : " . $count ;

foreach ($event_list as $event)

	// id
	//print $event->id . '<br />'; 
	// title
	print "<h2>Title  : ".$event->title . '</h2>'; 
	// where
	print "<b>Location of meeting  : </b>".$event->where[0] . '<br />'; 
	// description
	print "<b>Message  : </b>".$event->content . '<br />';	
	echo "<b>Start  : </b>".date_google_to_sql($event->when[0]->startTime) . "<br>" ; 
	echo "<b>End   : </b>".date_google_to_sql($event->when[0]->endTime) . "<br>" ; 
	//print $event->when[0]->endTime . '<br />'; 
	print '<hr><br />';
catch(Exception $e)
	// prevent Google username and password from being displayed
	// if a problem occurs
	echo "Could not connect to calendar.";

// from 	2010-06-11T07:30:00.000-07:00
// to 		2010-06-11 07:30:00
function date_google_to_sql($str) {
	$t = explode('T', $str); 
	$date = $t[0];
	$time = $t[1];
	$time = substr($time, 0, 8); 
	$str = $date . ' ' . $time;
	return $str;

Links :


1. Sharecash - March 1, 2012

You really make it seem really easy with your presentation but I find this matter to be actually something which I think I’d by no means understand. It kind of feels too complex and extremely huge for me. I’m looking forward on your subsequent submit, I will attempt to get the grasp of it!

tournasdimitrios1 - March 2, 2012

Thanks for commenting .
Yes , this article is targeted to visitors that feel comfortable with the Zend Framework . If you need specific explanation let me know , I’try to help you ..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s