How to import gmail contacts using php

How to import gmail contacts using php

Postby Neo » Tue Mar 09, 2010 8:47 am

Using this script, you can import the contact list in an array by passing correct username and password.
Place the following files and call the function in index.php with username and password to get the contacts list.

gmail_import.php
Code: Select all

<?php 
$location 
"";
$cookiearr = array();
$csv_source_encoding='utf-8';

#function get_contacts, accepts as arguments $login (the username) and $password
#returns array of: array of the names and array of the emails if login successful
#otherwise returns 1 if login is invalid and 2 if username or password was not specified
function get_contacts($login$password)
{
  
#the globals will be updated/used in the read_header function
  
global $csv_source_encoding;
  global 
$location;
  global 
$cookiearr;
  global 
$ch;

  
#check if username and password was given:
    
if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)==""))
    {
      
#return error code if they weren't
        
return 2;
    }
    
    
#initialize the curl session
    
$ch curl_init();
    
curl_setopt($chCURLOPT_URL,"https://www.google.com/accounts/ServiceLoginAuth?service=mail");
    
curl_setopt($chCURLOPT_REFERER"");
    
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    
curl_setopt($chCURLOPT_HEADERFUNCTION'read_header');
    
    
#get the html from gmail.com
  
$html curl_exec($ch);
    
    
$matches = array();
    
$actionarr = array();
    
    
$action "https://www.google.com/accounts/ServiceLoginAuth?service=mail";

    
#parse the login form:
    #parse all the hidden elements of the form
    
preg_match_all('/<input type="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)"[^>]*>/si'$html$matches);
    
$values $matches[2];
    
$params "";
    
    
$i=0;
    foreach (
$matches[1] as $name)
    {
      
$params .= "$name=" urlencode($values[$i]) . "&";
      ++
$i;
    }

  
$login urlencode($login);
  
$password urlencode($password);
  
  
#submit the login form:
    
curl_setopt($chCURLOPT_URL,$action);
    
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
    
curl_setopt($chCURLOPT_POST1);
    
curl_setopt($chCURLOPT_POSTFIELDS$params ."Email=$login&Passwd=$password&PersistentCookie=");

    
$html curl_exec($ch);

  
#test if login was successful:
  
if (!isset($cookiearr['GX']) && (!isset($cookiearr['LSID']) || $cookiearr['LSID'] == "EXPIRED"))
    {
        return 
1;
    }

  
#this is the new csv url:
  
curl_setopt($chCURLOPT_URL"http://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=GMAIL_CSV");
  
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
  
curl_setopt($chCURLOPT_HTTPGET1);

  
$html curl_exec($ch);
  
$html iconv ($csv_source_encoding,'utf-8',$html);

  
$csvrows explode("\n"$html);
  
array_shift($csvrows);
  
  
$names = array();
  
$emails = array();
    foreach (
$csvrows as $row)
    {
        if (
preg_match('/^((?:"[^"]*")|(?:[^,]*)).*?([^,@]+@[^,]+)/'$row$matches))
        {
            
$names[] = trim( ( trim($matches[1] )=="" ) ? current(explode("@",$matches[2])) : $matches[1] , '" ');
            
$emails[] = trim$matches[2] );
        }
    }
      

    return array(
$names$emails);
}

#read_header is essential as it processes all cookies and keeps track of the current location url
#leave unchanged, include it with get_contacts
function read_header($ch$string)
{
    global 
$location;
    global 
$cookiearr;
    global 
$ch;
    global 
$csv_source_encoding;

    
    
$length strlen($string);
    
    if (
preg_match("/Content-Type: text\\/csv; charset=([^\s;$]+)/",$string,$matches))
        
$csv_source_encoding=$matches[1];

    if(!
strncmp($string"Location:"9))
    {
      
$location trim(substr($string9, -1));
    }
    if(!
strncmp($string"Set-Cookie:"11))
    {
      
$cookiestr trim(substr($string11, -1));
      
$cookie explode(';'$cookiestr);
      
$cookie explode('='$cookie[0]);
      
$cookiename trim(array_shift($cookie)); 
      
$cookiearr[$cookiename] = trim(implode('='$cookie));
    }
    
$cookie "";
    if(
trim($string) == ""
    {
      foreach (
$cookiearr as $key=>$value)
      {
        
$cookie .= "$key=$value; ";
      }
      
curl_setopt($chCURLOPT_COOKIE$cookie);
    }

    return 
$length;
}

#function to trim the whitespace around names and email addresses
#used by get_contacts when parsing the csv file
function trimvals($val)
{
  return 
trim ($val"\" \n");
}

?>



index.php
Code: Select all

<pre>
<?php
include("gmail_import.php");

function getContacts($username = null, $password = null){

  $login = $username;
  $password =  $password;
  
  $resultarray 
= get_contacts($login, $password);
  
      foreach
($resultarray as $res){
        $emailArray['email'] = $res;
    }
  return $emailArray;
    
}

    print_r(getContacts("GMAIL_USERNAME","GMAIL_PASSWORD"));

?>
User avatar
Neo
Site Admin
Site Admin
 
Posts: 2452
Joined: Tue Jul 14, 2009 8:37 pm
Cash on hand: 190,465.80
Bank: 11,223,865.00
Location: Colombo/London
Medals: 2
EC_Bronze_Star (1) EC_Achievment (1)

Invitations sent: 94
Registered friends: 12
Highscores: 11
Reputation point: 67
Second Lieutenant

Return to PHP & MySQL

Who is online

Users browsing this forum: No registered users and 1 guest