GOOD NEWS PHP advice for displaying Google Calendar

Joined
May 22, 2013
Messages
301
Reaction score
44
Thread updated - new code available, see post #2.

On my Cisco 7941 I was recently displaying a Google Calendar in the menu.

The author of this code has said Google has deprecated the API it used and I now get an error trying to run the calendar.

This is the code I was using that has recently stopped:
Code:
<?php
//v1.50: Correctly orders results again (thanks, Tiffany in Oxnard CA, USA)
// fix for uninitialised variable (thanks, Daryl in Harrison OH, USA)
// debug now reports server's local time, which might be handy.
// potential fix for events appearing on wrong day (thanks, Ashok)
// replacement of deprecated eregi_replace (thanks, Sean in Iowa)
// Updated 5 February 2012
//v1.41: Small fix for max-results, which had disappeared. Thanks, Pete in Chicago
//v1.40: ...and now we have a cache, thanks to Emilio Velis who got @ramayac to
// write it for him. Which is excellent. Updated 21 February 2011
//v1.30: Daylight savings time *is*, honestly, now properly dealt with.
// thanks Kevin Grewohl for pointing out that it wasn't! ;)
// Updated 21 February 2011
//v1.20: Daylight savings time is now properly dealt with
//v1.10: This code now deals correctly with events that have a date range lasting more
// than one day. Thank you, David Power, for the fix!
//v1.01: Some small bugfixes//30 Jun 2010
//v1.00: Rewrite to remove SimplePie completely, and simply use PHP's inbuilt XML parser.
// Updated 29 June 2010
//v0.93: Added "make email addresses clickable". Thank you, Bjorn!
//v0.92: Fixed an issue with 'a section of dates' in amendable code. Thank you Kevin!
//v0.91: Nice error message if there are no events to display, requested by Tomas. Thanks!
//v0.90: Feature: clickable links in descriptions (start them [URL]http://[/URL]). Thank you, Adam!
// Feature: display end times, requested by Lucy. Thanks!
// Feature: group by date, requested by Lucy. Thanks!
// [URL]http://james.cridland.net/code[/URL]
 
 
/////////
//Configuration
//
 
// Your private feed - which you get by right-clicking the 'xml' button in the 'Private Address' section of 'Calendar Details'.
if (!isset($calendarfeed)) {$calendarfeed = "YOUR PRIVATE GOOGLE CALENDAR FEED HERE"; }
 
// Date format you want your details to appear
$dateformat="j F Y"; // 10 March 2009 - see [URL]http://www.php.net/date[/URL] for details
$timeformat="g.ia"; // 12.15am
 
// The timezone that your user/venue is in (i.e. the time you're entering stuff in Google Calendar.) [URL]http://www.php.net/manual/en/timezones.php[/URL] has a full list
date_default_timezone_set('Europe/London');
 
// How you want each thing to display.
// By default, this contains all the bits you can grab. You can put ###DATE### in here too if you want to, and disable the 'group by date' below.
$event_display=" ###TITLE###:
from ###FROM### ###DATESTART### until ###UNTIL### ###DATEEND###
###DESCRIPTION###
";
 
// What happens if there's nothing to display
$event_error="<P>There are no events to display.</p>";
 
// The separate date header is here
$event_dateheader="###DATE###";
$GroupByDate=true;
// Change the above to 'false' if you don't want to group this by dates.
 
// ...and how many you want to display (leave at 999 for everything)
$items_to_show=999;
 
// ...and here's where you tell it to use a cache.
// Your PHP will need to be able to write to a file called "gcal.xml" in your root. Create this file by SSH'ing into your box and typing these three commands...
// > touch gcal.xml
// > chmod 666 gcal.xml
// > touch -t 01101200 gcal.xml
// If you don't need this, or this is all a bit complex, change this to 'false'
$use_cache=false;
 
// And finally, change this to 'true' to see lots of fancy debug code
$debug_mode=false;
 
//
//End of configuration block
/////////
 
if ($debug_mode) {error_reporting (E_ALL); ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL); echo "<P>Debug mode is on. Hello there.<BR>Your server thinks the time is ".date(DATE_RFC822)."</p>";}
 
// Form the XML address.
$calendar_xml_address = str_replace("/basic","/full?singleevents=true&futureevents=true&max-results".$items_to_show."&orderby=starttime&sortorder=a",$calendarfeed); //This goes and gets future events in your feed.
 
if ($debug_mode) {
echo "<P>We're going to go and grab <a href='$calendar_xml_address'>this feed</a>.<P>";}
 
if ($use_cache) {
////////
//Cache
//
 
$cache_time = 3600*12; // 12 hours
$cache_file = $_SERVER['DOCUMENT_ROOT'].'/gcal.xml'; //xml file saved on server
 
if ($debug_mode) {echo "<P>Your cache is saved at ".$cache_file."</P>";}
 
$timedif = @(time() - filemtime($cache_file));
 
$xml = "";
if (file_exists($cache_file) && $timedif < $cache_time) {
if ($debug_mode) {echo "<P>I'll use the cache.</P>";}
$str = file_get_contents($cache_file);
$xml = simplexml_load_string($str);
} else { //not here
if ($debug_mode) {echo "<P>I don't have any valid cached copy.</P>";}
$xml = simplexml_load_file($calendar_xml_address); //come here
if ($f = fopen($cache_file, 'w')) { //save info
$str = $xml->asXML();
fwrite ($f, $str, strlen($str));
fclose($f);
if ($debug_mode) {echo "<P>Cache saved :)</P>";}
} else { echo "<P>Can't write to the cache.</P>"; }
}
 
//done!
} else {
$xml = simplexml_load_file($calendar_xml_address);
}
 
if ($debug_mode) {echo "<P>Successfully got the GCal feed.</p>";}
 
$items_shown=0;
$old_date="";
$xml->asXML();
 
foreach ($xml->entry as $entry){
$ns_gd = $entry->children('[URL]http://schemas.google.com/g/2005');[/URL]
 
//Do some niceness to the description
//Make any URLs used in the description clickable
$description = preg_replace('"\b([URL='http://\S+']http://\S [/URL])"', '<a href="$1">$1</a>', $entry->content);
 
// Make email addresses in the description clickable
$description = preg_replace("`([-_a-z0-9]+(\.[-_a-z0-9]+)*@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6})`i","<a href=\"mailto:\\1\" title=\"mailto:\\1\">\\1</a>", $description);
 
if ($debug_mode) { echo "<P>Here's the next item's start time... GCal says ".$ns_gd->when->attributes()->startTime." PHP says ".date("g.ia -Z",strtotime($ns_gd->when->attributes()->startTime))."</p>"; }
 
// These are the dates we'll display
$gCalDate = date($dateformat, strtotime($ns_gd->when->attributes()->startTime));
$gCalDateStart = date($dateformat, strtotime($ns_gd->when->attributes()->startTime));
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime));
$gCalStartTime = date($timeformat, strtotime($ns_gd->when->attributes()->startTime));
$gCalEndTime = date($timeformat, strtotime($ns_gd->when->attributes()->endTime));
 
// Now, let's run it through some str_replaces, and store it with the date for easy sorting later
$temp_event=$event_display;
$temp_dateheader=$event_dateheader;
$temp_event=str_replace("###TITLE###",$entry->title,$temp_event);
$temp_event=str_replace("###DESCRIPTION###",$description,$temp_event);
 
if ($gCalDateStart!=$gCalDateEnd) {
//This starts and ends on a different date, so show the dates
$temp_event=str_replace("###DATESTART###",$gCalDateStart,$temp_event);
$temp_event=str_replace("###DATEEND###",$gCalDateEnd,$temp_event);
} else {
$temp_event=str_replace("###DATESTART###",'',$temp_event);
$temp_event=str_replace("###DATEEND###",'',$temp_event);
}
 
$temp_event=str_replace("###DATE###",$gCalDate,$temp_event);
$temp_dateheader=str_replace("###DATE###",$gCalDate,$temp_dateheader);
$temp_event=str_replace("###FROM###",$gCalStartTime,$temp_event);
$temp_event=str_replace("###UNTIL###",$gCalEndTime,$temp_event);
$temp_event=str_replace("###WHERE###",$ns_gd->where->attributes()->valueString,$temp_event);
$temp_event=str_replace("###LINK###",$entry->link->attributes()->href,$temp_event);
$temp_event=str_replace("###MAPLINK###","[URL]http://maps.google.com/?q=[/URL]".urlencode($ns_gd->where->attributes()->valueString),$temp_event);
// Accept and translate HTML
$temp_event=str_replace("&lt;","<",$temp_event);
$temp_event=str_replace("&gt;",">",$temp_event);
$temp_event=str_replace("&quot;","\"",$temp_event);
 
if (($items_to_show>0 AND $items_shown<$items_to_show)) {
if ($GroupByDate) {if ($gCalDate!=$old_date) { echo $temp_dateheader; $old_date=$gCalDate;}}
echo $temp_event;
$items_shown++;
}
}
 
if (!$items_shown) { echo $event_error; }
?>

Now I figured since this is just an RSS feed I'm linking to I should surely be able to use Google's standard RSS feed, so I edited a basic RSS reader I already had, like this:
Code:
<?PHP
getFeed('HTTP://MY PRIVATE GOOGLE CALENDAR HERE');
function getFeed($feed_url) {
        $content = file_get_contents($feed_url);
        $x = new SimpleXmlElement($content);
    foreach($x->channel->item as $entry) {
        //$pgtitle = "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
        echo "" . $entry->title . "";
        echo  "
";
        echo "" . $entry->description . "";
        echo  "
 
";
    }
}
?>
All I get is a parse error with this.

Here's what Google say about the deprecation - https://developers.google.com/google-apps/calendar/v1/developers_guide_protocol

Can anyone help please? I know this isn't strictly a PIAF issue and more a Google/PHP issue but I'm hoping because it is phone oriented people here can help.

Thanks in advance.
 
Joined
May 22, 2013
Messages
301
Reaction score
44
Quick update - a good friend of mine has offered to lend me some of his coding expertise and has come up with the following code:

Code:
<?php
// By Untagged Limited - http://untagged.co.uk - Use, change, edit, distribute as you like.
$feedurl = "http://www.google.com/calendar/feeds/YOUR PERSONAL GOOGLE URL/basic?orderby=starttime&sortorder=a&futureevents=true&singleevents=true";
// Feed URL switches after ? are optional, Google has full list
$rss = simplexml_load_file($feedurl); // READ THE XML INTO PHP

foreach ($rss->entry as $item) {
$author_name= $item->author->name;
$published    = $item->published;
$updated    = $item->updated;
$title        = $item->title;
$summary    = $item->summary;   // THIS IS SHOWING MORE THAN 1 LINE OF TEXT DEPENDING ON THE INFORMATION STORED IN THE summary TAG.

$summary = explode("\n", $summary); // THE SUMMARY CONTAINS MULTIPLE LINES ONLY ON SOME EVENTS, THIS EXPLODES THE LINES

if ($summary[0] == "Recurring Event<br>") {
$duration = str_replace("Duration: ", "", $summary[3]);
$duration = $duration / 60 / 60;
$summary = "Recurring event, duration: ".$duration."h";
} else {
$summary = str_replace("When: ", "", $summary[0]);
}

$summary = str_replace(array("&nbsp;", "<br>"), "", $summary); // Strip out some tags

// TIME TO OUTPUT THE TEXT - JUST PUT // AT THE BEGINNING OF A LINE IF YOU WANT PHP TO IGNORE IT
// THE \n IS A "NEW LINE", SO AT THE END OF $summary I HAVE ADDED 2 X NEW LINES.
echo "$title\n";
echo "$summary\n\n";
//echo "Name: $author_name\n";
//echo "Published: $published\n";
//echo "Updated: $updated\n";
}
?>

Can anyone offer me further advice - I'm getting &npsb; at the end of every event on the phone, and <br> after recurring events. How would I strip this? (Update, thanks Lorne for post below, my friend has put in a stripping line so this issue is now solved).
 
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top