PHP Import Contacts From Gmail:
You can import all gmails contacts through php and its working in localhost also.
config.php
<?php
$clientid = 'client id';
$clientsecret = 'client secret key';
$redirecturi = 'http://localhost/gmail/result.php';
$maxresults = 50; // Number of mailid you want to display.
?>
Index.php
<?php require_once('config.php'); ?>
<a href="https://accounts.google.com/o/oauth2/auth?client_id=<?php print $clientid;?>&redirect_uri=<?php print $redirecturi; ?>&scope=https://www.google.com/m8/feeds/&response_type=code">Invite Friends From Gmail</a>
result.php
<?php
ini_set('display_errors', '1');
echo '<pre>';
require_once('config.php');
$authcode = $_GET["code"];
$post = array(
'code'=> urlencode($authcode),
'client_id'=> urlencode($clientid),
'client_secret'=> urlencode($clientsecret),
'redirect_uri'=> urlencode($redirecturi),
'grant_type'=> urlencode('authorization_code')
);
$query_string = '';
foreach($post as $key=>$value){
$query_string .= $key.'='.$value.'&';
}
$query_string = rtrim($query_string,'&');
$ch = curl_init();//open connection
curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$query_string);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
$accesstoken = $result->access_token;
if(!empty($accesstoken)) {
$_SESSION['token']= $accesstoken;
$contacts = file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?max-results='.$maxresults.'&oauth_token='. $_SESSION['token']);
$xml= new SimpleXMLElement($contacts);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
$count = 0;
foreach ($result as $title) {
$count++;
echo $count.". ".$title->attributes()->address . "<br><br>";
}
}
?>
No comments:
Post a Comment