我希望从谷歌联系人那里获取联系人的名字,没有任何运气.但是,我能够提取电子邮件地址没问题.有人能告诉我我做错了什么吗?
$xmlresponse=file_get_contents('https://www.google.com/m8/Feeds/contacts/default/full?oauth_token='.$accesstoken);
//reading xml using SimpleXML
$xml= new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$nameFirst = $xml->xpath('//gd:givenname'); // I have also tried //gd:name
$result = $xml->xpath('//gd:email');
foreach($nameFirst as $nameF){
echo $nameF->getName();
}
foreach ($result as $title) {
echo $title->attributes()->address . "<br>";
}
?>
解决方法:
我从Google Contacts API获得的XML是混合的,名称是普通XML节点“title”的节点值,但该电子邮件是gdata标记gd:email中的参数.考虑到多个电子邮件地址的可能性,我使用以下内容提取单个名称/电子邮件对的数组:
$req = new Google_HttpRequest("https://www.google.com/m8/Feeds/contacts/default/property-email/");
$val = $this->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, (string)$email->attributes()->address);
}
}