2010-06-09 5 views
1

J'essaie de récupérer les données XML pour Google Agenda. Authentification et récupération tous les travaux. Cependant, lorsque je récupère les événements, les données gd: ne sont pas incluses en tant que documents de référence du protocole (http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#RetrievingWithoutQuery)Récupération des événements de Google Agenda

Quelques messages d'erreur que je rencontre en fonction de la façon dont je référence le nœud "when" avec l'attribut "startTime" (mon but ultime dans cette anecdote) sont les suivants:

Erreur fatale: Appel à une fonction membre attributs() sur un non-objet dans .../googlecalendarwrapper.php sur la ligne 226 quand il ressemble à 'startTime'=> (string) $cal->when->attributes()->startTime,

GoogleCalendarWrapper_Model :: getEventsList() [googlecalendarwrapper-model.geteventslist]: Nœud n'existe plus quand il ressemble à 'startTime'=> strval($cal->when->attributes()->startTime),

strval() [function.strval]: nœud n'existe plus quand il ressemble à 'startTime'=> strval($cal->when->attributes()), et 'startTime'=> strval($cal->when->attributes('startTime')),

code

ressemble:

  $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] 
        ) 

      ) 

Répondre

4

Selon à cet article: http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/ Vous devez aborder les namespaces un peu différemment avec SimpleXM LElement. La solution est la suivante:

  $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), 
                 ); 
      } 

Notez le $ns_gd = $cal->children('http://schemas.google.com/g/2005'); - ce qui définit l'espace de noms. Puis à partir de là, $ns_gd->when->attributes()->startTime obtient l'attribut de gd: quand il est nommé startTime.

L'homme a été un sanglant 2 jours. Mais je l'ai compris. J'espère que cela peut aider quelqu'un sur la route.

Questions connexes