从Google日历中检索事件
我正在尝试检索Google日历的XML数据。认证和检索都可以。但是,当我检索事件时,gd:数据不包含在协议参考文档中(http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingWithoutQuery)从Google日历中检索事件
我遇到的一些错误消息取决于我如何引用“when”节点具有属性“startTime”(我在此轶事中的最终目标)如下:
致命错误:调用成员函数的属性()非线对象在.../googlecalendarwrapper.php上线226 它看起来像'startTime'=> (string) $cal->when->attributes()->startTime,
GoogleCalendarWrapper_Model :: getEventsList()[googlecalendarwrapper-model.geteventslist]:当它看起来像01时,节点不再存在()[function.strval]
strval:节点不再存在时,它看起来像'startTime'=> strval($cal->when->attributes()),
和'startTime'=> strval($cal->when->attributes('startTime')),
代码如下所示:
$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] => [email protected]
)
)
根据对这篇文章:http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/ 你必须用SimpleXM有点不同的名称空间LElement。解决方案如下:
$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),
);
}
请注意$ns_gd = $cal->children('http://schemas.google.com/g/2005');
- 它定义了命名空间。然后从那里,$ns_gd->when->attributes()->startTime
从命令startTime获取来自gd的属性:
男人这是一个血腥的2天。但我知道了。希望这可以帮助某个人。