php - 将 xpath 简单 xml 与 google 联系人 api 一起使用?

标签 php xml xpath

我正在使用 google contact 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) "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f991969e988a91d78d919c949c9f968b9c8a8db99e94989095d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>"
  }

原始 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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="543c3b3335273c7a203c313931323b26312720143339353d387a373b39" rel="noreferrer noopener nofollow">[email protected]</a>" primary="true" />
   </entry>

如何获取图像 URL 以及联系人姓名和职务?

最佳答案

您正在尝试访问<link>元素,但正在访问 attributes()$entry ,其中没有 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);

关于php - 将 xpath 简单 xml 与 google 联系人 api 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21236578/

相关文章:

php - 您的要求无法解析为一组可安装的软件包。使用 Composer

java - 即使我使用 AsyncTask 减轻主线程上的负载,应用程序也说没有响应

c# - 将 XML 文件保存在项目文件夹中

php - 基于属性值定位元素的XPath?

Python - 通过 Firefox 的 Tor 浏览器,无法单击按钮

php - 有没有办法在 PHP PDO 中使参数可选?

php - 如何使用kohana从数据库中获取数据?

xml - XSL 选择所有节点,而不是另一个节点的节点

xpath - Xpath,在其他两个节点之间查找节点

php - 更新购物车数量而不更新其他产品