Parsing Google XML Calendar

I have an XML feed from a public Google calendar. Looks like that:

<?xml version='1.0' encoding='UTF-8'?> <feed xmlns='................' xmlns:gd='http://schemas.google.com/g/2005'> <id>http://www.google.com/calendar/feeds/........./public/full</id> <updated>2009-08-24T10:57:00.000Z</updated> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/> <title type='text'>Sports Events</title> ..... <entry> <id>...........</id> <published>2009-08-14T00:29:58.000Z</published> <updated>2009-08-14T00:29:58.000Z</updated> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/> .............. <georss:where> <gml:Point> <gml:pos>11.111111 -22.22222</gml:pos> </gml:Point> </georss:where> <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'/> <gd:transparency value='http://schemas.google.com/g/2005#event.transparent'/> <gCal:uid value=' .........@google.com '/> <gCal:sequence value='0'/> <gd:when startTime='2009-10-03' endTime='2009-10-04'/> ............. 

Now in PHP:

 $calendar = simplexml_load_file('http://my.feed.com'); foreach ($calendar->entry as $item) { //here I get the whole <entry> node $gd = $item->children('http://schemas.google.com/g/2005'); // now in $gd I have all nodes like <gd:XXXXX> } 

The problem is how to get the value here?

 <gml:pos>11.111111 -22.22222</gml:pos> 

It is not in my $ gd variable, how to get this node?

+6
xml php simplexml
source share
4 answers

I highly recommend using this library: https://github.com/collegeman/coreylib . This will make everything from start to finish breathtakingly easy.

+10
source share

Of course, there are several ways to parse XML. Display Google Calendar events on your PHP website using XPath (see "Analyzing a Google Calendar Feed with PHP") and Integrating your PHP Application with Google Calendar are two comprehensive resources with sample code, etc.

Personally, I used the following approach:

 $doc = new DOMDocument(); $doc->load('http://my.feed.com'); $entries = $doc->getElementsByTagName("entry"); foreach ( $entries as $entry ) { $titles = $entry->getElementsByTagName("title"); $title = $titles->item(0)->nodeValue; $times = $entry->getElementsByTagName("when"); $startTime = $times->item(0)->getAttributeNode("startTime")->value; $when = date("l jS \o\f FY - h:i A", strtotime($startTime)); // ... } 

To access the georss namespace, etc., see (and its output)

 foreach ($doc->getElementsByTagNameNS('*', '*') as $element) { echo 'localName: ', $element->localName, ', prefix: ', $element->prefix, "\n"; } 
+5
source share

You can use the Zend GData library to analyze Google web services (including the calendar). This is easier than trying to use the XML code manually. There is a tutorial that shows how to do this here .

+1
source share

A complete calendar was much simpler than Coreylib. Corilib did not fail, although I am sure that he is very strong.

I had FullCalendar in 5 minutes. http://arshaw.com/fullcalendar/docs/google_calendar/

0
source share

All Articles