使用 PHP 解析 Google Contacts API XML

发布于 2024-11-17 04:48:57 字数 254 浏览 4 评论 0 原文

我有来自 Google Contacts API 的 xml feed,但在读取 标签时遇到问题。

$xml = simplexml_load_string($google_contacts);

这会读取除 标记之外的所有元素,其中包含电子邮件信息。

最好的做法是什么?

I have an xml feed from Google Contacts API, but I have trouble reading <gd: tags.

$xml = simplexml_load_string($google_contacts);

This reads every elements except the <gd: tags, which contains e-mail information.

What's the best thing to do?

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

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

发布评论

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

评论(4

忘东忘西忘不掉你 2024-11-24 04:48:57

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

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


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

对于第一次开始:

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

或者使用 DomDocument 来处理仅在 SimpleXML 中难以完成的部分:

$domElement = dom_import_simplexml($simpleXmlNode);

Update: If you're using name-spaces and xpath, you might need to register them first in SimpleXML.

If you have problems to access an element, try to enclose it within {} angel brackets, this probably helps: $xml->{gd:...}.


SimpleXML has no/limited support for XML-Namespaces if I remember correctly. DomDocument has, so take it instead.

For a first start:

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

Or use DomDocument for the part that is difficult to do in SimpleXML only:

$domElement = dom_import_simplexml($simpleXmlNode);
独留℉清风醉 2024-11-24 04:48:57

为了使其更易于使用,我所做的事情是通过将 alt=json 附加到请求字符串,将 gdata 返回为 json

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

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

然后它只是常规 json。

something that I did to make it easer to work with is I got the gdata back as json by appending alt=json to the request string

My full query string looks like:

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

Then it's just regular json.

溺ぐ爱和你が 2024-11-24 04:48:57

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

You can read "gd:" tags using xml_parse. It return all result in array, as xml string is not shared by you so you need to write further logic to get data from array

<?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

我乃一代侩神 2024-11-24 04:48:57

我遇到了同样的问题,无法访问 节点。
我制定了自己的解决方案,我需要每个条目下的完整联系人信息列表。
我猜这是因为每个 标记中存在冒号“:”,然后将其替换为无“:”,这就是我所做的:

$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='[email protected]' primary='true'/>
        <gd:email rel='http://schemas.google.com/g/2005#home' address='[email protected]'/>
        <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] => [email protected]
              [primary] => true
            )
        )
      [1] => Array(
          [@attributes] => Array(
              [rel] => http://schemas.google.com/g/2005#home
              [address] => [email protected]
            )
        )
    )
  [gdphoneNumber] => Array(
      [0] => 8888888888
      [1] => 9999999999
      [2] => 5555555555
    )
)

I had the same problem which could not access to <gd: nodes.
I make my own solution which I need the fUll list of contacts info under each entry.
I guess this is because of colon ":" in each <gd: tag then replace it with no ":" here is what I did:

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

this will make the following 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='[email protected]' primary='true'/>
        <gd:email rel='http://schemas.google.com/g/2005#home' address='[email protected]'/>
        <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>

to this print_r output of array:

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] => [email protected]
              [primary] => true
            )
        )
      [1] => Array(
          [@attributes] => Array(
              [rel] => http://schemas.google.com/g/2005#home
              [address] => [email protected]
            )
        )
    )
  [gdphoneNumber] => Array(
      [0] => 8888888888
      [1] => 9999999999
      [2] => 5555555555
    )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文