gpt4 book ai didi

php - 使用xpath简单xml与Google通讯录api?

转载 作者:行者123 更新时间:2023-12-03 16:18:48 26 4
gpt4 key购买 nike

我正在使用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) "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>


我如何获取图片网址以及联系人姓名和标题?

最佳答案

您试图访问<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com