【问题标题】:Google Contacts API get phone number (PHP)Google Contacts API 获取电话号码 (PHP)
【发布时间】:2015-07-14 12:03:15
【问题描述】:

我使用的是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 = 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 对象中找到它。

想法?

【问题讨论】:

    标签: php xml google-api google-api-php-client google-contacts-api


    【解决方案1】:

    Google Contacts API 使用 Atom 供稿。联系人以entry 元素的形式提供。所以迭代它们更有意义。为此,您还必须为 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);
    }
    

    Google Contacts API documentation 的第一个示例响应返回:

    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 元素,因此它是空的。一个联系人可以有多个电子邮件地址和/或电话号码,或者根本没有。提供rel 属性以将它们分类为家庭、工作......

    获取图片:

    图像作为具有特定 rel 属性的 Atom link 元素提供。

    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)

    【讨论】:

    • 感谢您的回答。我可以确认名称和电子邮件部分运作良好。无论我是否将access token 附加到这样的结果中,图像仍然给我带来问题:'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?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多