Showing posts with label Gmail Login using curl. Show all posts
Showing posts with label Gmail Login using curl. Show all posts

28 November, 2010

Codeigniter Library for getting Gmail Contact List

Today i needed to get the gmail contact list for a project that i am doing in codeigniter. For this is thought of making a library, so that it can be used easily. So, here in the complete library class fr getting gmail contact list.

if (!defined('BASEPATH')) exit('No direct script access allowed');

class GmailContacts_lib {

var $FEED_URL = "http://www.google.com/m8/feeds/contacts/default/full";
var $LOGIN_URL = "https://www.google.com/accounts/ClientLogin";
var $username;
var $passwd;
var $postData = array();

function GmailContacts_lib($gUsername, $gPassword) {
//constructor function
$this->username = $gUsername;
$this->passwd = $gPassword;
}

function get_gmail_contacts() {
$emailLists = array();
//create an array for post data
$this->postData = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => $this->username,
"Passwd" => $this->passwd,
"service" => "cp",
"source" => "anything"
);
//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;
}

//function to set curl options
function set_curl_options($ch, $option, $value) {
//make the post TRUE
return curl_setopt($ch, $option, $value);
}

//function to parse response
public 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['email_1']=$child->getAttribute('address');
elseif(strpos($child->getAttribute('rel'),'work')!=false)
$tempArray['email_2']=$child->getAttribute('address');
elseif(strpos($child->getAttribute('rel'),'other')!==false)
$tempArray['email_3']=$child->getAttribute('address');
}
break;
} //end of switch for nodeNames
} //end of foreach for entry_nodes child nodes
if( !empty($tempArray['email_1'])) $contacts[$tempArray['email_1']] = $tempArray;
if( !empty($tempArray['email_2'])) $contacts[$tempArray['email_2']] = $tempArray;
if( !empty($tempArray['email_3'])) $contacts[$tempArray['email_3']] = $tempArray;
}
return $contacts;
}
}

And thats it. Hope this helps someone like me.

06 July, 2010

Get Google Calendar Events using cURL

Once i needed to get all the google calendar events to my site, i.e. getting all the events that we added to google calendar to our custom calendar. After some reading, i found some helpful tips for doing this and here is the code. This is just a snippet. For logging in to gmail account please check my other post. The authentication token obtained after successful login is used in the following code.
So, what this code does is, it gets google calendar events using cURL. The code is simple. Just the retrieving of xml feed of google calendar events is done here, after this we can parse the xml and display all the events to our custom page after some formatting. Checkout the code.

$fAuth = "authentication-token-obtained-after-successful-login";
$arr_headers = array();
$arr_headers[] = "Host: www.google.com";
$arr_headers[] = "MIME-Version: 1.0";
$arr_headers[] = "Accept: text/xml";
$arr_headers[] = "Authorization: GoogleLogin auth=".$fAuth;
$arr_headers[] = "Content-type: application/atom+xml";
$arr_headers[] = "Cache-Control: no-cache";
$arr_headers[] = "Connection: Keep Alive \r\n";

$url = "http://www.google.com/calendar/feeds/emialadd%40gmail.com/private/full";
$ch = curl_init();


curl_setopt($ch, CURLOPT_REFERER, "http://google.com");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20040612 Mozilla Firebird/0.9");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$resFin = curl_exec($ch);
echo $resFin;
?>

14 May, 2010

Login to Gmail with curl

Following code is used to login to gmail using curl. The code is quite simple, for curl options please check php manual.
After loging in to gmail with curl provided below, user can then get their contact list of calendar. Only the logging in step is given
in this code. If the gmail login is successfull, then a unique auth key is obtained and if login fails then nothing is returned.

//store username and password and some values in array
$clientPost = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "gmailusername",
"Passwd" => "gmailpassword",
"service" => "cp",
"source" => "leaveurdream" // you can name it anything you want
);
//get the gmail login url
$clientUrl = "https://www.google.com/accounts/ClientLogin";
//initialize the curl object
$curl = curl_init($clientUrl);
//set curl option to indicate that we are doing POST
curl_setopt($curl, CURLOPT_POST, true);
//pass the post data array as parameters
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientPost);
//set the following line for SSL and authentication
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//set option for not checking the peer's certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//do not echo the data in the browser but save it as string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//get the response after executing our curl
$response = curl_exec($curl);

//perform a regular expression match to get the auth key from $response variable
//if the auth key is found then user is logged in successfully else login has failed
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
$auth = $matches[1];
//echo the auth key
echo $auth;
?>