代码之家  ›  专栏  ›  技术社区  ›  bradenkeith

从谷歌日历中检索事件

  •  1
  • bradenkeith  · 技术社区  · 14 年前

    我正在尝试检索谷歌日历的XML数据。认证和检索所有工作。但是,当我检索事件时,gd:data不作为协议引用文档包含在其中( http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingWithoutQuery )

    我遇到的一些错误消息取决于我如何引用具有属性“starttime”的“when”节点(我在本轶事中的最终目标),如下所示:

    致命错误:在第226行的…/googlecalendarwrapper.php中对非对象调用成员函数attributes()。 当它看起来像 'startTime'=> (string) $cal->when->attributes()->startTime,

    googleCalendarWrapper_model::getEventsList()[googleCalendarWrapper model.getEventsList]:节点看起来不再存在 'startTime'=> strval($cal->when->attributes()->startTime),

    strval()[function.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] => braden.keith@smartersys.com
                        )
    
                )
    
    1 回复  |  直到 14 年前
        1
  •  4
  •   bradenkeith    14 年前

    根据本文: http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/ 必须使用simplexmlement稍微改变名称空间的处理方式。解决方法如下:

                $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 从gd:获取命名为starttime时的属性。

    伙计,这是血腥的两天。但我明白了。希望这能帮上忙。