2

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

$xml = simplexml_load_string($google_contacts);

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

最好的办法是什么?

4

4 回答 4

2

更新:如果您使用名称空间和 xpath,您可能需要先在 SimpleXML 中注册它们。

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


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

第一次开始:

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

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

$domElement = dom_import_simplexml($simpleXmlNode);
于 2011-06-22T16:40:00.873 回答
2

我为使其更易于使用而做的事情是通过附加alt=json到请求字符串将 gdata 恢复为 json

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

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

然后它只是普通的json。

于 2015-07-22T19:39:37.277 回答
1

您可以使用 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

于 2011-06-22T16:52:59.010 回答
0

我有同样的问题,无法访问<gd:节点。我制作了自己的解决方案,我需要每个条目下的完整联系人信息列表。我猜这是因为每个<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 Contact 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
    )
)
于 2013-08-28T07:29:32.110 回答