从 Google 日历检索事件

发布于 2024-09-04 21:49:58 字数 5203 浏览 3 评论 0原文

我正在尝试检索 Google 日历的 XML 数据。身份验证和检索一切正常。但是,当我检索事件时,gd: data 并未作为协议参考文档包含在内(http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetreeringWithoutQuery

我遇到的一些错误消息取决于我的方式我引用带有属性“startTime”的“when”节点(我在这个轶事中的最终目标)如下:

致命错误:在.../googlecalendarwrapper.php中的非对象上调用成员函数attributes()第226行 当它看起来像 'startTime'=>; (字符串)$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] => [email protected]
                    )

            )

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] => [email protected]
                    )

            )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2024-09-11 21:49:58

根据这篇文章:http://www.sitepoint。 com/blogs/2005/10/20/simplexml-and-namespaces/
您必须使用 SimpleXMLElement 稍微不同地处理名称空间。解决方案如下:

            $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:when 命名为 startTime 获取属性。

老兄,这两天真是血淋淋的。但我想通了。希望这可以帮助某人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文