I am trying to retrieve the XML data for Google Calendar. Authentication and retrieval all works. However, when I retrieve the events, gd: data isn't included as the protocol reference documents it would be (http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingWithoutQuery)
Some error messages I'm running into depending on how I'm referencing the "when" node with attribute "startTime" (my ultimate goal in this anecdote) are as follows:
Fatal error: Call to a member function attributes() on a non-object in .../googlecalendarwrapper.php on line 226
when it looks like 'startTime'=> (string) $cal->when->attributes()->startTime,
GoogleCalendarWrapper_Model::getEventsList() [googlecalendarwrapper-model.geteventslist]: Node no longer exists when it looks like 'startTime'=> strval($cal->when->attributes()->startTime),
strval() [function.strval]: Node no longer exists when it looks like 'startTime'=> strval($cal->when->attributes()),
and 'startTime'=> strval($cal->when->attributes('startTime')),
Code looks like:
$xml = new SimpleXMLElement($this->get($url, $header));
$calendars = array();
foreach ($xml->entry as $cal){
$calendars[] = array(
'id'=>strval($cal->id),
'published'=>strval($cal->published),
'updated'=>strval($cal->updated),
'title'=>strval($cal->title),
'content'=>strval($cal->content),
'link'=>strval($cal->link->attributes()->href),
'authorName'=>strval($cal->author->name),
'authorEmail'=>strval($cal->author->email),
'startTime'=> strval($cal->when->attributes()),
);
}
XML:
[0] => SimpleXMLElement Object
(
[id] => http://www.google.com/calendar/feeds/braden.keith%40smartersys.com/private/full/7li4mr2c81mub1hcoqktn73fbo
[published] => 2010-06-08T17:17:43.000Z
[updated] => 2010-06-08T17:17:43.000Z
[category] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
开发者_开发百科 [term] => http://schemas.google.com/g/2005#event
)
)
[title] => title
[content] => content
[link] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => alternate
[type] => text/html
[href] => https://www.google.com/calendar/hosted/smartersys.com/event?eid=N2xpNG1yMmM4MW11YjFoY29xa3RuNzNmYm8gYnJhZGVuLmtlaXRoQHNtYXJ0ZXJzeXMuY29t
[title] => alternate
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/calendar/feeds/braden.keith%40smartersys.com/private/full/7li4mr2c81mub1hcoqktn73fbo
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => edit
[type] => application/atom+xml
[href] => https://www.google.com/calendar/feeds/braden.keith%40smartersys.com/private/full/7li4mr2c81mub1hcoqktn73fbo/63411700663
)
)
)
[author] => SimpleXMLElement Object
(
[name] => Braden Keith
[email] => braden.keith@smartersys.com
)
)
According to this article: http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/ You have to approach namespaces a tad differently with SimpleXMLElement. The solution is as follows:
$xml = new SimpleXMLElement($this->get($url, $header));
$xml->asXML();
$calendars = array();
foreach ($xml->entry as $cal){
$ns_gd = $cal->children('http://schemas.google.com/g/2005');
$calendars[] = array(
'id'=>strval($cal->id),
'published'=>strval($cal->published),
'updated'=>strval($cal->updated),
'title'=>strval($cal->title),
'content'=>strval($cal->content),
'link'=>strval($cal->link->attributes()->href),
'authorName'=>strval($cal->author->name),
'authorEmail'=>strval($cal->author->email),
'startTime'=> strval($ns_gd->when->attributes()->startTime),
);
}
Note the $ns_gd = $cal->children('http://schemas.google.com/g/2005');
- this defines the namespace. Then from there, $ns_gd->when->attributes()->startTime
gets the attribute from gd:when named startTime.
Man it's been a bloody 2 days. But I figured it out. Hopefully this can help someone down the road.
精彩评论