【问题标题】:using xpath simple xml with google contacts api?将 xpath 简单 xml 与谷歌联系人 api 一起使用?
【发布时间】:2014-01-20 14:31:32
【问题描述】:

我正在使用谷歌联系人 api 和 php 来解析 xml,如下所示:

$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
$val = $client->getIo()->authenticatedRequest($req);
$xml = simplexml_load_string($val->getResponseBody());
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');

  $output_array = array();
  foreach ($xml->entry as $entry) {
    foreach ($entry->xpath('gd:email') as $email) {
      $output_array[] = array(
        (string)$entry->title, 
        //THIS DOESNT WORK WHY??
        (string)$entry->attributes()->href, 
        //
       (string)$email->attributes()->address);

    }
  }

这会返回:

[1]=>
  array(3) {
    [0]=>
    string(14) "LOREM IPSUM"
    [1]=>
    string(0) ""
    [2]=>
    string(28) "hogash.themeforest@gmail.com"
  }

原始的 xml 响应是这样的:

     <entry>
      <id>http://www.google.com/m8/feeds/contacts/EMAIL/base/e29c818b038d9a</id>
      <updated>2012-08-28T21:52:20.909Z</updated>
      <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
      <title type="text">Lorem ipsum</title>
      <link rel="http://schemas.google.com/contacts/2008/rel#edit-photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/EMAIL/e29c818b038d9a/1B2M2Y8AsgTpgAmY7PhCfg" />
      <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/EMAIL/full/e29c818b038d9a" />
      <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/EMAIL/full/e29c818b038d9a/1346190740909001" />
      <gd:email rel="http://schemas.google.com/g/2005#other" address="hogash.themeforest@gmail.com" primary="true" />
   </entry>

如何获取图片网址以及联系人姓名和职务?

【问题讨论】:

  • 您正试图从三个&lt;link&gt; 标签中获取href
  • 只是 image type="image/*" 就足够了,但三个都很好......

标签: php xml xpath


【解决方案1】:

您正在尝试访问&lt;link&gt; 元素,但正在访问$entryattributes(),它没有href 属性。

// Doesn't have an href attribute...
(string)$entry->attributes()->href

相反,获取link 元素并循环它们以创建href 数组。

  $output_array = array();
  foreach ($xml->entry as $entry) {
    // Initialize an array out here.
    $entry_array = array();

    // Get the title and link attributes (link as an array)
    $entry_array['title'] = (string)$entry->title;

    $entry_array['hrefs'] = array();
    foreach($entry->link as $link) {
      // append each href in a loop
      $entry_array['hrefs'][] = $link->attributes()->href;
    }

    // If there are never more than 1 email, you don't need a loop here.
    foreach ($entry->xpath('gd:email') as $email) {
      // Get the email
      $entry_array['email'] = (string)$email->attributes()->address
    }
    // Append your array to the larger output
    $output_array[] = $entry_array;
  }

  // Look at the result
  print_r($output_array);

【讨论】:

  • 你真是个天才,谢谢你我已经和这个 API 斗争了一天半了,这让我发疯了......
  • 很好,知道如何获取图像吗?
  • @ToniMichelCaubet 图像在&lt;link&gt; 标记中应该在foreach ($entry-&gt;link...) 循环内。要找出它在循环时是什么,请使用if ($link-&gt;attributes()-&gt;type == "image/*")
  • 感谢您的回答,但我不明白,这是$link转储object(SimpleXMLElement)#30 (1) { ["@attributes"]=&gt; array(3) { ["rel"]=&gt; string(38) "http://schemas.google.com/g/2005#other" ["address"]=&gt; string(22) "rocio_deia@hotmail.com" ["primary"]=&gt; string(4) "true" } }
  • @ToniMichelCaubet SimpleXMLElement objects 实现 var_dump() 以显示其中包含的一些信息。它有一个@attributes 键,因此您可以访问$link['@attributes']['address'] 来获取该电子邮件地址。在我的示例中,我使用了attributes() 方法,而不是从$link 获取href 和类型。
猜你喜欢
  • 2011-10-09
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
相关资源
最近更新 更多