问题
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'>
<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 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?
回答1:
I highly recommend using this library: https://github.com/collegeman/coreylib. It'll make everything from start to finish mind-numbingly easy.
回答2:
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";
}
回答3:
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.
回答4:
Full Calendar was MUCH easier than Coreylib. Coreylib wasn't drop dead simple, although I'm sure it is very powerful.
I had FullCalendar running in 5 minutes. http://arshaw.com/fullcalendar/docs/google_calendar/
来源:https://stackoverflow.com/questions/1347033/parsing-google-calendar-xml-feed