Anche se ci sono centinaia di script che permettono di importare i contatti Gmail, quello che vi proponiamo oggi è davvero semplice e facile da estendere.
Nel tutorial di oggi imparerete come creare una classe PHP che recupera i contatti Gmail.
< ?php
ini_set("display_errors","2");
error_reporting(E_ERROR | E_WARNING | E_PARSE);
require_once(‘mvc/controllers/GmailController.php’);
$GmailController = new GmailController(‘email@gmail.com’,‘xxxx’);
$contacts = $GmailController->getContacts();
if($contacts){
print ‘
‘;
print_r($contacts);
print ‘
‘;
} else {
print ‘did not work’;
}
?>
< ?php
class GmailController {
private $FEED_URL = “http://www.google.com/m8/feeds/contacts/default/full”;
private $LOGIN_URL = “https://www.google.com/accounts/ClientLogin”;
private $username;
private $passwd;
private $postData = array();
public function __construct($gUsername, $gPassword) {
//constructor function
$this->username = $gUsername;
$this->passwd = $gPassword;
}
public function getContacts() {
$emailLists = array();
//create an array for post data
$this->postData = array(
“accountType” => “HOSTED_OR_GOOGLE”,
“Email” => $this->username,
“Passwd” => $this->passwd,
“service” => “cp”,
“source” => “my_source”
);
//initialize the curl object
$curl = curl_init($this->LOGIN_URL);
//set the curl options
$this->set_curl_options($curl, CURLOPT_POST, true);
$this->set_curl_options($curl, CURLOPT_POSTFIELDS, $this->postData);
$this->set_curl_options($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$this->set_curl_options($curl, CURLOPT_SSL_VERIFYPEER, false);
$this->set_curl_options($curl, CURLOPT_RETURNTRANSFER, 1);
//following variable contains the responses
$response = curl_exec($curl);
//check if the user has logged in sucessfully
//and save auth key if logged in
preg_match(“/Auth=([a-z0-9_\-]+)/i”, $response, $matches);
$auth = $matches[1];
if( !empty($auth)) {
$headers = array(“Authorization: GoogleLogin auth=”.$auth);
//make the request to google contacts feed with the auth key maximum contacts is 10000
$curl1 = curl_init($this->FEED_URL);
//passing the headers of auth key
$this->set_curl_options($curl1, CURLOPT_HTTPHEADER, $headers);
//return the result in a variable
$this->set_curl_options($curl1, CURLOPT_RETURNTRANSFER, 1);
//results response
$feed = curl_exec($curl1);
//parse the feed and return email list array
$emailLists = $this->parse_response($feed);
}
else {
$emailLists = array(“Invalid Username/Password”);
}
return $emailLists;
}
private function set_curl_options($ch, $option, $value) {
return curl_setopt($ch, $option, $value);
}
private function parse_response($feed) {
$contacts = array();
$doc = new DOMDocument();
//load the XML response
$doc->loadXML($feed);
//check the entry tag
$nodeList = $doc->getElementsByTagName(‘entry’);
foreach($nodeList as $node) {
//children of each entry tag
$entry_nodes = $node->childNodes;
$tempArray = array();
foreach($entry_nodes as $child) {
//get the tagname of the child
$domNodesName = $child->nodeName;
switch($domNodesName) {
case “title”:{ $tempArray['fullname'] = $child->nodeValue; }
break;
case “gd:email”:{
if (strpos($child->getAttribute(‘rel’),’home’)!==false)
$tempArray['homeemail']=$child->getAttribute(‘address’);
if(strpos($child->getAttribute(‘rel’),’work’)!=false)
$tempArray['workemail']=$child->getAttribute(‘address’);
if(strpos($child->getAttribute(‘rel’),’other’)!==false)
$tempArray['otheremail']=$child->getAttribute(‘address’);
}
break;
} //end of switch for nodeNames
} //end of foreach for entry_nodes child nodes
if( !empty($tempArray['homeemail'])) $contacts[$tempArray['homeemail']] = $tempArray;
if( !empty($tempArray['workemail'])) $contacts[$tempArray['workemail']] = $tempArray;
if( !empty($tempArray['otheremail'])) $contacts[$tempArray['otheremail']] = $tempArray;
}
return $contacts;
}
}
?>
fonte: www.sastgroup.com ? Vai al post originale






