Cla*_*cea 4 php api simplexml contacts
我希望从谷歌联系人那里获取联系人的名字,没有任何运气.但是,我能够提取电子邮件地址没问题.有人能告诉我我做错了什么吗?
$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>";
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 5
我从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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2930 次 |
| 最近记录: |