Recupération de la liste des contacts d'un compte gmail

Bossnours Messages postés 2 Date d'inscription vendredi 1 juin 2012 Statut Membre Dernière intervention 1 juin 2012 - 1 juin 2012 à 10:37
Bossnours Messages postés 2 Date d'inscription vendredi 1 juin 2012 Statut Membre Dernière intervention 1 juin 2012 - 1 juin 2012 à 12:25
Bonjour,
je cherche actuellement a récupérer la liste des contacts d'un compte Gmail. Actuellement mon code ne me rapporte que les adresses mail mais pas les noms associés aux adresses.
voici mon code:
$email = 'toto@gmail.com';
$password = 1234';

$result = retrieveGmailContactEmails($email,$password);
if (!$result) print("erreur dans le traitement");
else ?><?php print_r($result)?>

<?php;


function retrieveGmailContactEmails($login,$pass) {
                                
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "accountType=GOOGLE&service=cp&source=unknow&Email=".urlencode($login)."&Passwd=".urlencode($pass));
  $auth = curl_exec($ch);                                             
  preg_match("/Auth=(.*?)$/", $auth, $m);
  if (!$m) return false;
  else $auth = $m[1];
                                                    
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "//http://www.google.com/m8/feeds/contacts/".urlencode($login)."/full");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=".$auth));
  $result = curl_exec($ch);

  preg_match_all("/address='(.*?)'/", $result, $emails);
  if (!$emails) return ;
                                                                                                                          
  return $emails[1];
}


Quelqu'un a une idée de modification ou une autre façon de faire?

1 réponse

Bossnours Messages postés 2 Date d'inscription vendredi 1 juin 2012 Statut Membre Dernière intervention 1 juin 2012
1 juin 2012 à 12:25
Bon j'ai trouver une autre solution qui fonctionne je vous la partage:
(il faut installer zend sur votre serveur)

<?php
// set credentials for ClientLogin authentication
$user = $_POST['email'];
$pass = $_POST['password'];
?>
<!DOCTYPE html 
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Listing contacts</title>
<style>
body
{
font-family: Verdana;      
}
div.name
{
color: red; 
text-decoration: none;
font-weight: bolder;  
}
div.entry
{
display: inline;
float: left;
width: 400px;
height: 150px;
border: 2px solid; 
margin: 10px;
padding: 5px;
}
td 
{
vertical-align: top;
}
</style>    
</head>

     
    <?php
// load Zend Gdata libraries
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Feed');



try
{
// perform login and set protocol version to 3.0
$client = Zend_Gdata_ClientLogin::getHttpClient(
$user, $pass, 'cp');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);

// perform query and get result feed
$query = new Zend_Gdata_Query(
'http://www.google.com/m8/feeds/contacts/default/full?max-results=10000');
$feed = $gdata->getFeed($query);

// display title and result count
?>


<?php echo $feed->title; ?>




<?php echo $feed->totalResults; ?> contact(s) found.



<?php
// parse feed and extract contact information
// into simpler objects
$results = array();

foreach($feed as $entry)
{
$xml = simplexml_load_string($entry->getXML());
$obj = new stdClass;
$obj->name = (string) $entry->title;
$obj->orgName = (string) $xml->organization->orgName; 
$obj->orgTitle = (string) $xml->organization->orgTitle; 

foreach ($xml->email as $e)
{
$obj->emailAddress[] = (string) $e['address'];
}

foreach ($xml->phoneNumber as $p)
{
$obj->phoneNumber[] = (string) $p;
}
foreach ($xml->website as $w)
{
$obj->website[] = (string) $w['href'];
}

$results[] = $obj;  
}
} 
catch (Exception $e)
{
die('ERROR:' . $e->getMessage());  
}
    ?>
    
    <?php
    // display results
    foreach ($results as $r)
{
?>


  
<?php echo (!empty($r->name)) ? 
   $r->name : 'Name not available'; ?>

  

Organization,
<?php echo $r->orgName; ?>,

----

Email,
<?php echo @join(', ', $r->emailAddress); ?>,

----

Phone,
<?php echo @join(', ', $r->phoneNumber); ?>,

----

Web,
<?php echo @join(', ', $r->website); ?>

  



<?php
    }
    ?>

  
</html>
0
Rejoignez-nous