Google通讯录API获取电话号码(PHP) [英] Google Contacts API get phone number (PHP)

查看:192
本文介绍了Google通讯录API获取电话号码(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用,我可以提取姓名和电子邮件地址,但我想也可以获取个人资料照片和电话号码。



我使用PHP,这是我的身份验证代码:

  //如果验证成功... 

$ 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'); //成功获取人员的电子邮件地址

echo $ email-> parentNode-> getElementsByTagName('title') - > item(0) - > textContent; //成功获取人名


$ c

电话号码



这部分电话号码不会有效。

  $ phone = $ xpath-> query('// gd:phoneNumber'); 

foreach($ phone作为$ row){

print_r($ row); //此部分不工作

$ b $ / code>

PROFILE PICTURE



从上面的API链接可以看出,我还可以从网址抓取个人资料图片: https:// www .google.com / m8 / feeds / contacts / default / full 但我不确定如何在 DOMXPath <$ c中找到它

想法?

解决方案

Google Contacts 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')作为$ 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'=> [],
'数字'=> []
];
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文档返回:

  array(3){
[name] =>
字符串(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 属性将它们归类为home,work,...

获取图像: h2>

图像以特定的 rel 链接元素提供c>属性。



atom:link [@ rel =http://schemas.google.com/contacts/2008/rel#photo ]



这将返回链接节点,但可以获取直接使用href属性:



atom:link [@ rel =http://schemas.google.com/contacts/2008/rel#照片] / @ href



将属性节点列表转换为字符串:

字符串(原子:连结[@的rel = http://schemas.google.com/contacts/2008/rel#photo] / @ HREF)


I'm using the Google Contacts API and I'm able to extract names and email addresses but I'd like to also get profile pictures and phone numbers.

I'm using PHP and here's my code while authenticating:

//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 NUMBER

This part getting the phone number doesn't work.

$phone = $xpath->query('//gd:phoneNumber');

foreach ( $phone as $row ){

  print_r($row); // THIS PART DOESNT WORK

}

PROFILE PICTURE

Judging from the API link above, it looks like I can also grab the profile picture from the URL: https://www.google.com/m8/feeds/contacts/default/full but I'm not sure how to find it within the DOMXPath $xpath object I generated.

Thoughts?

解决方案

The Google Contacts API uses an Atom feed. The contacts are provided as entry elements. So it makes more sense the iterate them. To do that you have to register a prefix for the atom namespace as well.

$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');

If you use DOMXpath::evaluate() you can use expressions that return scalars. The second argument is a context node for the expression.

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);
}

With the first example response from the Google Contacts API documentation this returns:

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"
  }
}

The example does not include an email element, so it is empty. A contact can have multiple email addresses and/or phone numbers or none at all. The rel attribute is provided to classify them as home, work, ...

Fetching the image:

The image is provided as an Atom link element with a specific rel attribute.

atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]

This would return the link node, but it is possible to fetch the href attribute directly:

atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href

Cast the attribute node list into a string:

string(atom:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]/@href)

这篇关于Google通讯录API获取电话号码(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆