Invite Friends from Gmail

Posted: March 8, 2014 in Php
Tags: , ,


Google has introduced OAuth 2.0 and OAuth 1.0 has been officially deprecated as of April 20, 2012. So let us create a program to invite friends from Gmail using OAuth 2.0. Before you can use OAuth 2.0, you must register your application using the  Google APIs Console. First you need to create a project and follow the steps.

Create a New project:

Then Click on API Access to Create an OAuth 2.0 Client ID

Fill up the form to create your project

Setting your Client ID

Now you will get your Client ID, Client secret and Redirect URIs that we will use in our project. You can edit your Redirect URIs by clicking on Edit Setting in the right side. Once you have all info lets create our conig.php file.


In this file you have to enter Client ID, Client secret and Redirect URIs.

$clientid = 'Your Client here';
$clientsecret = 'Your Client Secret';
$redirecturi = 'http://your path/result.php'; 
$maxresults = 50; // Number of mailid you want to display.


<?php include('config.php'); ?>

<a href="
<?php print $clientid;?>&redirect_uri=<?php print $redirecturi; ?>
Invite Friends From Gmail</a>



$authcode = $_GET["code"];
'code'=> urlencode($authcode),
'client_id'=> urlencode($clientid),
'client_secret'=> urlencode($clientsecret),
'redirect_uri'=> urlencode($redirecturi),
'grant_type'=> urlencode('authorization_code') );

$fields_string = '';
foreach($fields as $key=>$value){ $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');

$ch = curl_init();//open connection
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

$response = json_decode($result);
$accesstoken = $response->access_token;
if( $accesstoken!='')
$_SESSION['token']= $accesstoken;
$xmlresponse= file_get_contents('
default/full?max-results='.$maxresults.'&oauth_token='. $_SESSION['token']);

$xml= new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', '');
$result = $xml->xpath('//gd:email');
$count = 0;
foreach ($result as $title) {
echo $count.". ".$title->attributes()->address . "<br><br>";

Download all files from here

Download Project Files


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s