Google 联系人 API 获取电话号码(PHP)

3

我正在使用Google联系人API,我能够提取姓名和电子邮件地址,但我也想获取个人资料图片和电话号码。

我正在使用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 = $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 但是我不确定如何在我生成的$xpath对象中找到它。

有什么想法吗?

1个回答

3
谷歌联系人API使用Atom feed。联系人以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(),可以使用返回标量的表达式。第二个参数是表达式的上下文节点。
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联系人API文档中的第一个示例响应,返回如下内容:
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属性来对它们进行分类,如家庭、工作等。

获取图片:

图片以Atom link元素形式提供,具有特定的rel属性。 atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"] 这将返回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)

感谢您的回答。我可以确认姓名和电子邮件部分运行良好。无论我是否将“访问令牌”附加到结果中,像这样:'image' => $xpath->evaluate('string(atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href)', $entry). '&access_token='.urlencode($accesstoken),图像仍然给我带来问题。您能帮忙吗? - tim peterson
1
如果我理解文档正确的话,您必须使用Google_HttpRequest对象发送授权的GET请求到图像URL以获取它。就像获取XML feed一样,但将响应存储/传递为二进制图像数据。我的示例是否从XML feed中读取有效的图像URL? - ThW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接