3
votes

For the past four hours I've been researching how to parse xml and echo out a google calendar feed. I easily accomplished this at first using this tutorial but ran into a problem with namespace, I could easily print out the summary and title of events but I was unable to print out startTime and endTime because in the feed they use <gd:when startTime> and etc. I found coreylib and heard much praise about it's simplicity but I still cannot figure out how to do this.

Could someone please point me in the direction or give me a sample code of retrieving some events from a "full" google feed (http://www.google.com/calendar/feeds/[email protected]/public/full - an example feed). Even if it just retrieved the title of the event and startTime that would be more than enough to work with.

Usually I post code but in this case I have hardly any because I'm stuck that bad haha. Thank you in advance!

2

2 Answers

4
votes
$email = "yourEmail";
$url = "http://www.google.com/calendar/feeds/".$email."/public/full";
$xml = file_get_contents($url);

$feed = simplexml_load_string($xml);
$ns=$feed->getNameSpaces(true);

foreach ($feed->entry as $entry) {
    $when=$entry->children($ns["gd"]);
    $when_atr=$when->when[0]->attributes();
    $start=$when_atr['startTime'];
    $end=$when_atr['endTime'];
    $title=$entry->title;

    echo "<p>".$start." - ".$end." ".$title."</p>";
}

Did tons more searched and found something similar to this, it works beautifully if anyone every needs it for something similar. Wooo!

2
votes

Based on what you have above I was able to modify it bit to adjust the styling.

The PHP

<?php 
$email = "your public calendar address email";
$url = "http://www.google.com/calendar/feeds/".$email."/public/full";
$xml = file_get_contents($url);

$feed = simplexml_load_string($xml);
$ns=$feed->getNameSpaces(true);

foreach ($feed->entry as $entry) {
    $when=$entry->children($ns["gd"]);
    $when_atr=$when->when[0]->attributes();

    $title=$entry->title;
    echo "<div class='eventTitle'>".$title . "</div>";

    $start = new DateTime($when_atr['startTime']);
    echo "<div class='eventTime'>".$start->format('D F jS, g:ia') . " to ";    

    $end = new DateTime($when_atr['endTime']);
    echo $end->format('g:ia')."</div>" . '<br />' ;    



}

 ?>

The CSS

<style>

 .eventTime {
    color:#0CF;
 }

 .eventTitle {
    color:#0FC; 
 }

 </style>

The following resources were helpful:

Date, Date and Time, and the Example @Glavić posted.