Php 将xpath简单xml与google contacts api一起使用?

Php 将xpath简单xml与google contacts api一起使用?,php,xml,xpath,Php,Xml,Xpath,我使用google contacts 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-&g

我使用google contacts 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);

    }
  }
     <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>
这将返回:

[1]=>
  array(3) {
    [0]=>
    string(14) "LOREM IPSUM"
    [1]=>
    string(0) ""
    [2]=>
    string(28) "hogash.themeforest@gmail.com"
  }
原始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);

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

http://www.google.com/m8/feeds/contacts/EMAIL/base/e29c818b038d9a
2012-08-28221:52:20.909Z
乱数假文

如何获取图像url以及联系人姓名和标题

您试图访问
元素,但正在访问
$entry
属性()
,该属性没有
href
属性

// Doesn't have an href attribute...
(string)$entry->attributes()->href
相反,获取
链接
元素并在其上循环以创建
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);

您正试图从三个
标签中获取
href
?仅使用image type=“image/*”就足够了,但这三个标签都很好…您是个天才谢谢我与此API斗争了一天半,这让我抓狂…很好,知道如何获取图像吗?@tonimichel打赌图像位于
标记中,应该位于
foreach($entry->link…
循环中。要找出循环时它是什么,请使用类似于
if($link->attributes()->type==“image/*”)的东西,谢谢你的回答,但我不明白,这就是
$link
转储
对象(simplexmlement)#30(1){[@attributes”]=>数组(3){[“rel”]=>字符串(38)”http://schemas.google.com/g/2005#other"     [“地址”]=>字符串(22)“rocio_deia@hotmail.com“[“primary”]=>string(4)“true”}
@ToniMichelCaubet implement
var_dump()
以显示其中包含的一些信息。它有一个@attributes键,因此您可以访问
$link['@attributes']['address']
获取该电子邮件地址。在我的示例中,我使用
attributes()
方法从
$link
获取href和type。