Wednesday, April 18, 2012

PHP, extract information from Google calendar



We can use Google  Data APIs to extract Google calendar data and other Google application.
The Google Data APIs are a set of web services for read/write access to applications hosted by Google. Zend_Gdata provides PHP 5 client interfaces for the Google Data APIs.

Download Zend Gdata package from:
 http://framework.zend.com/download/gdata

 After unzip the data and extract the data, the directory like this:
ZendGdata-1.11.11
I copy the  ZendGdata-1.11.11/library/Zend to the Zend directory under  my web root directory.
Below is the PHP test code to extract  public Google calendar event title and time in 8 days for Gmail account test@gmail.com
 <?php
 //ini_set('display_errors', 1);
//error_reporting(E_ALL);

require_once($_SERVER['DOCUMENT_ROOT'].'/Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
$service = new Zend_Gdata_Calendar();
$query = $service->newEventQuery();

$query->setUser('test@gmail.com');

$query->setVisibility('public');
$query->setProjection('full');
$query->setStartMin(date('Y-n-j'));
$query->setStartMax(date('Y-n-j', time() + (60*60 *24*8)));
$query->setOrderby('starttime');

try { $eventFeed = $service->getCalendarEventFeed($query); }
catch (Zend_Gdata_App_Exception $e) { return; }
echo "<ul>\n";
  foreach ($eventFeed as $event) {
    echo "\t<li>" . $event->title->text .  " \n";
    echo "\t\t<ul>\n";
    foreach ($event->when as $when) {
      echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
    }
    echo "\t\t</ul>\n";
    echo "\t</li>\n";
  }
  echo "</ul>\n";

?>
To access the private calendar with authentication: (replace test@gmail.com and test_password with true Gmail user name and password)
<?php
 ini_set('display_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/Vancouver');
require_once($_SERVER['DOCUMENT_ROOT'].'/Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $user = "test@gmail.com";
    $pass = "test_password ";
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
    $service = new Zend_Gdata_Calendar($client);

$query = $service->newEventQuery();
$query->setVisibility('private');
$query->setProjection('full');
$query->setStartMin(date('Y-n-j'));
$query->setStartMax(date('Y-n-j', time() + (60*60 *24*8)));
$query->setOrderby('starttime');

try { $eventFeed = $service->getCalendarEventFeed($query); }
catch (Zend_Gdata_App_Exception $e) { return; }
echo "<ul>\n";
  foreach ($eventFeed as $event) {
    echo "\t<li>" . $event->title->text .  " \n";
    echo "\t\t<ul>\n";
    foreach ($event->when as $when) {
      echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
    }
    echo "\t\t</ul>\n";
    echo "\t</li>\n";
  }
  echo "</ul>\n";


?>

To insert event in your Google calendar:
<?php
 ini_set('display_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/Vancouver');
require_once($_SERVER['DOCUMENT_ROOT'].'/Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $user = "test@gmail.com";
    $pass = "test_password";
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
    $service = new Zend_Gdata_Calendar($client);

$query = $service->newEventQuery();


$query->setVisibility('private');
$query->setProjection('full');
$query->setStartMin(date('Y-n-j'));
$query->setStartMax(date('Y-n-j', time() + (60*60 *24*8)));
$query->setOrderby('starttime');

try { $eventFeed = $service->getCalendarEventFeed($query); }
catch (Zend_Gdata_App_Exception $e) { return; }
echo "<ul>\n";
  foreach ($eventFeed as $event) {
    echo "\t<li>" . $event->title->text .  " \n";
    echo "\t\t<ul>\n";
    foreach ($event->when as $when) {
      echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
    }
    echo "\t\t</ul>\n";
    echo "\t</li>\n";
  }
  echo "</ul>\n";

$newEvent = $service->newEventEntry();
$title="Jiansen Test";
$newEvent->title = $service->newTitle($title);
$where ="cesei";
$newEvent->where = array($service->newWhere($where));
$description="This is a test";
$newEvent->content = $service->newContent("$description");
$startDate="2012-04-19";
$endDate="2012-04-19";
$startTime = '10:00';
$endTime = '11:00';
$tzOffset = '-08';
$when = $service->newWhen();
$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
$newEvent->when = array($when);
$who = $service->newwho();
$who->setEmail('test@gmail.com');

$SendEventNotifications = new Zend_Gdata_Calendar_Extension_SendEventNotifications();
 $SendEventNotifications->setValue(true);
$newEvent->SendEventNotifications = $SendEventNotifications;
    

       
$newEvent->setWho(array($who));
// Upload the event to the calendar server
// A copy of the event as it is recorded on the server is returned
    // This event will occur all day every Friday starting from 9th July 2010 until 8th Dec 2015
    /*
    $recurrence = "DTSTART;VALUE=DATE:20100709\r\n" .
            "DTEND;VALUE=DATE:20100710\r\n" .
            "RRULE:FREQ=WEEKLY;BYDAY=Fr;UNTIL=20151208\r\n";
    
    $newEvent->recurrence = $service->newRecurrence($recurrence);
*/

$createdEvent = $service->insertEvent($newEvent);


?>

No comments:

Post a Comment