我们可以得到(至少)用户'联系人列表中gmail联系人的图像吗?


Can we get the image for (at least) the gmail contacts found in the user's contact list?

我现在正在接收电子邮件和姓名,像这样:

function tratar_gmail($accesstoken){
    $max_results = 3000;
    $url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
    //echo $url;
    $xmlresponse =  curl_file_get_contents($url);
    if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0))
    {
        echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>";
        exit();
    }
    $xml =  new SimpleXMLElement($xmlresponse);
    $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
    $result = $xml->xpath('//gd:email');
    $return;
    foreach ($result as $title) {
      $parts = explode('@', $title->attributes()->address);

      $pos = count($return);
      $return[$pos]['email'] = $title->attributes()->address;
      $return[$pos]['name'] = strip_tags($parts[0]);
    }

Where $title dumps:

object(SimpleXMLElement)#4 (1) {   ["@attributes"]=>   array(3) {     ["rel"]=>     string(38) "http://schemas.google.com/g/2005#other"     ["address"]=>     string(18) "xxxxxxx@wanadoo.es"     ["primary"]=>     string(4) "true"   } }

但是如果能得到一些图片就太好了,至少对于gmail联系人来说,有办法吗?

类似:

$return[$pos]['email'] = $title->attributes()->address;
$return[$pos]['name'] = strip_tags($parts[0]);
if (preg_match('/^gmail.com/', $return[$pos]['emal']) {
     $return[$pos]['pic'] = magic_goes_here()
}

照片链接可以在联系人条目中找到:

<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gContact='http://schemas.google.com/contact/2008'
    xmlns:gd='http://schemas.google.com/g/2005'
    gd:etag='{contactEtag}'>
  <id>
    http://www.google.com/m8/feeds/contacts/{userEmail}/base/{contactId}
  </id>
  ...
  <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*'
    href='https://www.google.com/m8/feeds/photos/media/{userEmail}/{contactId}'
    gd:etag='{photoEtag}'/>

如果没有照片,则link元素将不具有gd:etag属性。

要检索每个联系人的图像,请获取URL:

https://www.google.com/m8/feeds/photos/media/{userEmail}/{contactId}

Google Contacts API的完整文档