I have been attempting to parse Google Calendar so that the events on the calendar will display on my website. For some reason, the code never returns an event. The only message that will display is “There are no events scheduled.” I have tried linking to other Google calendars that are also public, and I receive the same message. Any help at all is greatly appreciated!
parse php:
[php]
<?
include 'Event.php';
putenv("TZ=US/Mountain");
$calendarURL = 'http://www.google.com/calendar/feeds/[email protected]/ public/full';
$startRange = "yesterday";
$endRange = "next month";
$rangeStartSecs = strtotime($startRange);
$searchTime = strftime("%G-%m-%dT%H:%M:%S", $rangeStartSecs);
$searchTime = '?start-min=' . $searchTime;
$rangeEndSecs = strtotime($endRange);
$searchTime = $searchTime . '&start-max=';
$searchTime = $searchTime . strftime("%G-%m-%dT%H:%M:%S", $rangeEndSecs);
$data = file_get_contents($calendarURL . $searchTime);
$data = str_replace("\xe2\x80\x99","`",$data);
$data = str_replace('&','and', $data);
$readTitle = false;
$readDescription = false;
$tmpEvent = NULL;
$events = Array();
$announcements = Array();
$xmlParser = xml_parser_create();
xml_set_element_handler($xmlParser, "tagOpen", "tagClose");
xml_set_character_data_handler($xmlParser, "tagData");
xml_parse($xmlParser, $data);
$eventsOrder = Array();
foreach ($events as $event)
{
if ($event->recurs)
{
sort($event->recurArray);
$event->startTime = $event->recurArray[0];
}
array_push($eventsOrder,$event->startTime);
}
asort($eventsOrder);
header("Content-type: text/xml");
echo "<calendar>";
foreach ($eventsOrder as $order => $val)
{
$events[$order]->toString();
}
foreach ($announcements as $announcement)
{
$announcement->toString();
}
echo '</calendar>';
function tagOpen($parser, $tag, $attribs)
{
global $rangeStartSecs;
global $tmpEvent;
global $readTitle;
global $readDescription;
if ($tag == 'ENTRY')
{
$tmpEvent = new Event();
}
else if ($tag == 'TITLE')
{
$readTitle = true;
}
else if ($tag == 'CONTENT')
{
$readDescription = true;
}
else if ($tag == 'GD:WHEN')
{
if ($tmpEvent->recurs)
{
if (strtotime($attribs['STARTTIME']) > $rangeStartSecs)
{
array_push($tmpEvent->recurArray,strtotime($attribs['STARTTIME']));
$tmpEvent->endTime = strtotime($attribs['ENDTIME']);
}
}
else
{
$tmpEvent->startTime = strtotime($attribs['STARTTIME']);
$tmpEvent->endTime = strtotime($attribs['ENDTIME']);
}
}
else if ($tag == 'GD:WHERE')
{
$tmpEvent->where = $attribs['VALUESTRING'];
}
else if ($tag == 'GD:RECURRENCE')
{
$tmpEvent->recurs = true;
}
}
function tagData($parser, $data)
{
global $tmpEvent;
global $readTitle;
global $readDescription;
if ($readDescription == true)
{
if ($data[0] == "@")
{
$tmpEvent->announcement = true;
$tmpEvent->description = substr($data,1-strlen($data));
}
else
{
$tmpEvent->description = $data;
}
}
if ($readTitle == true)
{
$tmpEvent->title = $data;
$readTitle = false;
}
}
function tagClose($parser, $tag)
{
global $tmpEvent;
global $events;
global $announcements;
global $readDescription;
if ($tag == 'ENTRY')
{
if ($tmpEvent->announcement)
array_push($announcements,$tmpEvent);
else
array_push($events,$tmpEvent);
}
if ($tag == 'CONTENT')
{
$readDescription = false;
}
}
?>
[/php]
event php:
[php]
<?
class Event
{
var $title;
var $where;
var $description;
var $startTime;
var $endTime;
var $timeString;
var $recurs;
var $recurArray;
var $announcement;
function Event()
{
$this->recurs = false;
$this->announcement = false;
$this->recurArray = Array();
}
function toString()
{
echo '<event>';
echo '<title>' . $this->title . '</title>';
$this->parseWhen();
echo '<when>' . $this->timeString . '</when>';
echo '<where>' . $this->where . '</where>';
echo '<description>' . $this->description . '</description>';
echo '</event>';
}
function parseWhen()
{
if ($this->recurs)
{
if (strftime("%l",($this->recurArray[0])) ==
(strftime("%l",$this->endTime)))
{
$recur = strftime("Every %A",$this->recurArray[0]);
$this->timeString = $recur;
}
else
{
$recur = strftime("Every %A, %l:%M%p", $this->recurArray[0]);
$recur = $recur . ' - ' . strftime("%l:%M%p", $this->endTime);
$this->timeString = $recur;
}
}
else if ($this->announcement == true)
{
$this->timeString = "Announcement";
}
else
{
if (strftime("%l",($this->startTime)) == (strftime("%l",$this->endTime)))
{
$this->timeString = strftime("%A %B %e, %G",$this->startTime);
}
else
{
$start = strftime("%A %B %e, %G %l:%M%p", $this->startTime);
$end = strftime("%l:%M%p", $this->endTime);
$this->timeString= $start . ' - ' . $end;
}
}
}
}
?>
[/php]
javascript:
var xmlhttp;
function loadCalendar()
{
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject)
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","gcalParse.php", true);
xmlhttp.onreadystatechange = stateChanged;
xmlhttp.send('');
}
function stateChanged()
{
if (xmlhttp.readyState == 1)
{
loadingText="<br/><div align='center'><img src='busy.gif'/></div><br/>";
document.getElementById('insertCalendar').innerHTML = loadingText;
}
else if (xmlhttp.readyState == 4)
{
updatePage();
}
}
function updatePage()
{
var xml = xmlhttp.responseXML.documentElement;
document.getElementById('insertCalendar').innerHTML = "";
var html = "<div id='calendar'>";
var events = xml.getElementsByTagName('event');
if (events.length == 0)
{
html += 'No events scheduled.</div>';
}
else
{
var title;
var when;
var where;
var description;
for (var i=0; i < events.length; i++)
{
title = events[i].getElementsByTagName('title').item(0).firstChild.data;
when = events[i].getElementsByTagName('when').item(0).firstChild.data;
try
{
var map=events[i].getElementsByTagName('where').item(0).firstChild.data;
var encoded = encodeURIComponent(map);
where = "Location: ";
if (parseInt(map))
{
where += "<a href='http://maps.google.com/?q=" + encoded + "'>";
where += map + "</a>";
}
else
{
where += map;
}
}
catch (e)
{
where = '';
}
try
{
description =
events[i].getElementsByTagName('description').item(0).firstChild.data;
}
catch (e)
{
description = '';
}
html += "<div class='subhead'>"+title+"</div>";
html += "<div class='bodycopy'><i>"+when+"</i></div>";
html += "<div class='bodycopy'>"+where+"</div>";
html += "<br/><div class='bodycopy'>"+description+"</div>";
if (i != events.length - 1)
html += "<hr style='width:95%'/>";
}
html += '</div>';
}
document.getElementById('insertCalendar').innerHTML = html;
}
Example of the code in my site to display the php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>My Site</title>
<script language="javaScript" type="text/javascript" src="gcal.js"></script>
<link rel="stylesheet" href="gcal.css" type="text/css" media="screen, projection">
</head>
<body onload="loadCalendar()">
<div id='insertCalendar'></div>
</body>
</html>