tags:

views:

1788

answers:

3

Hi

I've got XML feed from public google calendar. Looks like this:

<?xml version='1.0' encoding='UTF-8'?>
    <feed xmlns='................' xmlns:gd='http://schemas.google.com/g/2005'&gt;
     <id>http://www.google.com/calendar/feeds/........./public/full&lt;/id&gt;
     <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'/&gt;
      <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'/&gt;
..............
   <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'/&gt;
   <gd:transparency value='http://schemas.google.com/g/2005#event.transparent'/&gt;
   <gCal:uid value='[email protected]'/>
   <gCal:sequence value='0'/>
   <gd:when startTime='2009-10-03' endTime='2009-10-04'/>

.............

Now to 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 value from here?

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

Its not present in my $gd variable, how to get this node?

+4  A: 

I highly recommend using this library: http://coreylib.com. It'll make everything from start to finish mind-numbingly easy.

Kenneth Reitz
Oh this is just brilliant. Thank you for showing me this library :)
6bytes
Haha my pleasure! it's great, isn't it?
Kenneth Reitz
That is very impressive
David Caunt
+1  A: 

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

Phyxx
Thanks but coreylib worked like magic :)
6bytes
A: 

There are of course several ways to parse the XML. Display Google Calendar events on your PHP Web site with XPath (see "Parsing the Google Calendar feed with PHP") and Integrate your PHP application with Google Calendar are two comprehensive resources with sample code and more.

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 F Y - h:i A", strtotime($startTime));
  // ...
}

In order to access the georss namespace etc. have a look at (and its output)

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