Google 联系人 API 获得 phone 号码 (PHP)
Google Contacts API get phone number (PHP)
我正在使用 Google Contacts API,我可以提取姓名和电子邮件地址,但我还想获取个人资料图片和 phone 号码。
我正在使用 PHP,这是我验证时的代码:
//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 数字
这部分获取 phone 号码 不 工作。
$phone = $xpath->query('//gd:phoneNumber');
foreach ( $phone as $row ){
print_r($row); // THIS PART DOESNT WORK
}
个人资料照片
从上面的 API link 来看,我似乎也可以从 URL: https://www.google.com/m8/feeds/contacts/default/full
中获取个人资料图片,但我不确定如何在我生成的 DOMXPath
$xpath
对象中找到它。
想法?
Google 联系人 API 使用 Atom 订阅源。联系人以 entry
元素的形式提供。所以迭代它们更有意义。为此,您还必须为原子命名空间注册一个前缀。
$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(),则可以使用 return 标量的表达式。第二个参数是表达式的上下文节点。
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);
}
对于来自 Google Contacts API documentation 这个 return 的第一个示例响应:
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"
}
}
该示例不包含 email
元素,因此它是空的。一个联系人可以有多个电子邮件地址 and/or phone 号码或 none。 rel
属性用于将它们分类为家庭、工作、...
正在获取图像:
图像作为具有特定 rel
属性的 Atom link
元素提供。
atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]
这将 return link
节点,但可以直接获取 href 属性:
atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href
将属性节点列表转换为字符串:
string(atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href)
我正在使用 Google Contacts API,我可以提取姓名和电子邮件地址,但我还想获取个人资料图片和 phone 号码。
我正在使用 PHP,这是我验证时的代码:
//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 数字
这部分获取 phone 号码 不 工作。
$phone = $xpath->query('//gd:phoneNumber');
foreach ( $phone as $row ){
print_r($row); // THIS PART DOESNT WORK
}
个人资料照片
从上面的 API link 来看,我似乎也可以从 URL: https://www.google.com/m8/feeds/contacts/default/full
中获取个人资料图片,但我不确定如何在我生成的 DOMXPath
$xpath
对象中找到它。
想法?
Google 联系人 API 使用 Atom 订阅源。联系人以 entry
元素的形式提供。所以迭代它们更有意义。为此,您还必须为原子命名空间注册一个前缀。
$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(),则可以使用 return 标量的表达式。第二个参数是表达式的上下文节点。
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);
}
对于来自 Google Contacts API documentation 这个 return 的第一个示例响应:
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"
}
}
该示例不包含 email
元素,因此它是空的。一个联系人可以有多个电子邮件地址 and/or phone 号码或 none。 rel
属性用于将它们分类为家庭、工作、...
正在获取图像:
图像作为具有特定 rel
属性的 Atom link
元素提供。
atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]
这将 return link
节点,但可以直接获取 href 属性:
atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href
将属性节点列表转换为字符串:
string(atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href)