谷歌联系人API获取电话号码(PHP)
我正在使用,我可以提取姓名和电子邮件地址,但我也想获得个人资料图片和电话号码 我正在使用PHP,以下是我在验证时的代码:谷歌联系人API获取电话号码(PHP),php,xml,google-api,google-api-php-client,google-contacts-api,Php,Xml,Google Api,Google Api Php Client,Google Contacts Api,我正在使用,我可以提取姓名和电子邮件地址,但我也想获得个人资料图片和电话号码 我正在使用PHP,以下是我在验证时的代码: //if authenticated successfully... $req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full"); $val = $client->getIo()->authenticatedRequest($req); $doc
//if authenticated successfully...
$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
$val = $client->getIo()->authenticatedRequest($req);
$doc = new DOMDocument;
$doc->recover = true;
$doc->loadXML($val->getResponseBody());
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('gd', 'http://schemas.google.com/g/2005');
$emails = $xpath->query('//gd:email');
foreach ( $emails as $email ){
echo $email->getAttribute('address'); //successfully gets person's email address
echo $email->parentNode->getElementsByTagName('title')->item(0)->textContent; //successfully gets person's name
}
电话号码
获取电话号码的这一部分不起作用
$phone = $xpath->query('//gd:phoneNumber');
foreach ( $phone as $row ){
print_r($row); // THIS PART DOESNT WORK
}
个人资料图片
从上面的API链接判断,我似乎还可以从URL获取配置文件图片:https://www.google.com/m8/feeds/contacts/default/full
但我不确定如何在我生成的DOMXPath
$xpath
对象中找到它
想法?谷歌联系人API使用Atom提要。触点作为
条目
元素提供。因此,迭代它们更有意义。要做到这一点,还必须为atom命名空间注册前缀
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$xpath->registerNamespace('gd', 'http://schemas.google.com/g/2005');
如果使用DOMXpath::evaluate(),则可以使用返回标量的表达式。第二个参数是表达式的上下文节点
foreach ($xpath->evaluate('/atom:feed/atom:entry') as $entry) {
$contact = [
'name' => $xpath->evaluate('string(atom:title)', $entry),
'image' => $xpath->evaluate('string(atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href)', $entry),
'emails' => [],
'numbers' => []
];
foreach ($xpath->evaluate('gd:email', $entry) as $email) {
$contact['emails'][] = $email->getAttribute('address');
}
foreach ($xpath->evaluate('gd:phoneNumber', $entry) as $number) {
$contact['numbers'][] = trim($number->textContent);
}
var_dump($contact);
}
对于来自的第一个示例响应,this返回:
array(3) {
["name"]=>
string(17) "Fitzwilliam Darcy"
["image"]=>
string(64) "https://www.google.com/m8/feeds/photos/media/userEmail/contactId"
["email"]=>
string(0) ""
["numbers"]=>
array(1) {
[0]=>
string(3) "456"
}
}
该示例不包含电子邮件
元素,因此为空。联系人可以有多个电子邮件地址和/或电话号码,或者根本没有。提供的rel
属性可将它们分类为家庭、工作
正在获取图像:
图像作为一个具有特定rel
属性的Atomlink
元素提供
atom:link[@rel=”http://schemas.google.com/contacts/2008/rel#photo“]
这将返回链接
节点,但可以直接获取href属性:
atom:link[@rel=”http://schemas.google.com/contacts/2008/rel#photo“]/@href
将属性节点列表强制转换为字符串:
string(原子:链接[@rel=”http://schemas.google.com/contacts/2008/rel#photo“]/@href)
谢谢你的回答。我可以确认名称和电子邮件部分工作良好。不管我是否将访问令牌
附加到如下结果中,图像仍然给我带来问题:'image'=>$xpath->evaluate('string(atom:link[@rel='http://schemas.google.com/contacts/2008/rel#photo“]/@href]”,$entry)。”&access_token='.urlencode($accesstoken)
您能帮忙吗?如果我正确理解了文档,您必须使用Google_HttpRequest
对象向图像url发送授权GET请求以获取它。就像获取XML提要一样,但是将响应存储/交付为二进制图像数据。我的示例是否从XML提要读取外观有效的图像URL?