【问题标题】:Google Contacts API XML parsing with PHP使用 PHP 解析 Google Contacts API XML
【发布时间】:2011-09-20 13:32:39
【问题描述】:

我有一个来自 Google 联系人 API 的 xml 提要,但我无法读取 <gd: 标签。

$xml = simplexml_load_string($google_contacts);

这会读取除包含电子邮件信息的<gd: 标签之外的所有元素。

最好的办法是什么?

【问题讨论】:

  • 我会继续使用不同的 XML 解析器。将某些东西与当前的 xml 与对象解析器结合在一起最终可能会使某些东西变得丑陋。
  • 以 DomDocument 代替,它支持 xml 命名空间。
  • 请显示一些您尝试访问这些元素的代码。 reads not 具体是什么意思?

标签: php gdata google-data-api google-contacts-api


【解决方案1】:

更新:如果您使用命名空间和 xpath,您可能会在 SimpleXML 中使用 need to register them first

如果您在访问某个元素时遇到问题,请尝试将其括在 {} 尖括号内,这可能会有所帮助:$xml->{gd:...}


如果我没记错的话,SimpleXML 对 XML 命名空间没有/有限的支持。 DomDocument 有,所以取而代之。

第一次开始:

$dom = new DomDocument;
$dom->loadXML($google_contacts);
...

或者对于仅在 SimpleXML 中难以完成的部分使用 DomDocument:

$domElement = dom_import_simplexml($simpleXmlNode);

【讨论】:

    【解决方案2】:

    您可以使用 xml_parse 读取“gd:”标签。它返回数组中的所有结果,因为您不共享 xml 字符串,因此您需要编写进一步的逻辑以从数组中获取数据

    <?php
    $content = $google_contacts;
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $content, $data, $index);
    xml_parser_free($parser);
    print"<pre>";
    print_r($data);
    /*foreach($data as $vals )
    {    
      //write your code to get result from array
    }*/
    ?>
    

    http://www.php.net/manual/en/function.xml-parse.php

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,无法访问&lt;gd: 节点。 我制作了自己的解决方案,我需要每个条目下的完整联系人信息列表。 我猜这是因为每个&lt;gd: 标签中的冒号“:”然后用没有“:”替换它这是我所做的:

      $search=array('<gd:','</gd:');
      $replace=array('<gd','</gd');
      /*Create json object from $google_contacts(atom xml string)
      by replacing "gd:" to "gd"*/
      $response = json_encode(str_replace($search, $replace,$google_contacts));
      /*Create Array from $response*/
      $contactsList = json_decode($response, true);
      print "<pre>" . print_r($contactsList['entry'], true);
      

      这将使以下 Google 联系人 xml:

      <? xml version = '1.0' encoding = 'UTF-8' ?>
      <feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>
      
          ...
      
          <entry>
              <id>http://www.google.com/m8/feeds/contacts/xxxxxxxx%40gmail.com/base/0</id>
              <updated>2012-09-23T01:54:56.646Z</updated>
              <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
              <title type='text'>Will Jordan</title>
              <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSXN'/>
              <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0'/>
              <link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0'/>
              <link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1348365xxxxxx'/>
              <gd:email rel='http://schemas.google.com/g/2005#other' address='yyy@gmail.com' primary='true'/>
              <gd:email rel='http://schemas.google.com/g/2005#home' address='zzz@yahoo.com'/>
              <gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>8888888888</gd:phoneNumber>
              <gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile'>9999999999</gd:phoneNumber>
              <gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>5555555555</gd:phoneNumber>
              <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/4fde39d984'/>
              <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/3f5dc5d3be0'/>
          </entry>
      </feed>
      

      到这个数组的 print_r 输出:

      array(
      [id] => http://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/base/0
        [updated] => 2013-08-27T19:33:22.431Z
        [category] => Array(
            [@attributes] => Array(
                [scheme] => http://schemas.google.com/g/2005#kind
                [term] => http://schemas.google.com/contact/2008#contact
              )
          )
        [title] => Will Jordan
        [link] => Array(
            [0] => Array(
                [@attributes] => Array(
                    [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo
                    [type] => image/ *
                    [href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSX
                  )
              )
            [1] => Array(
                [@attributes] => Array(
                    [rel] => http://schemas.google.com/contacts/2008/rel#photo
                    [type] => image/ *
                    [href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0
                  )
              )
            [2] => Array(
                [@attributes] => Array(
                    [rel] => self
                    [type] => application/atom+xml
                    [href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0
                  )
              )
            [3] => Array(
                [@attributes] => Array(
                    [rel] => edit
                    [type] => application/atom+xml
                    [href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1377632
                  )
              )
          )
        [gdemail] => Array(
            [0] => Array(
                [@attributes] => Array(
                    [rel] => http://schemas.google.com/g/2005#other
                    [address] => yyy@gmail.com
                    [primary] => true
                  )
              )
            [1] => Array(
                [@attributes] => Array(
                    [rel] => http://schemas.google.com/g/2005#home
                    [address] => zzz@yahoo.com
                  )
              )
          )
        [gdphoneNumber] => Array(
            [0] => 8888888888
            [1] => 9999999999
            [2] => 5555555555
          )
      )
      

      【讨论】:

        【解决方案4】:

        为了让工作更轻松,我所做的事情是通过将 alt=json 附加到请求字符串中,将 gdata 作为 json 恢复

        我的完整查询字符串如下所示:

        https://www.google.com/m8/feeds/contacts/{userEmail}/full?oauth_token=token&max-results=9999999&alt=json');
        

        那么就是普通的json。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-12
          • 1970-01-01
          • 2010-11-10
          • 2017-02-11
          • 1970-01-01
          相关资源
          最近更新 更多