Fetch Google Contacts using Google API – Codeigniter

I developed an application as a feature of that application i need to fetch the gmail contacts of my  users , I researched many blogs and articles in Google and finally i developed that feature….

Here in this blog i posted How to fetch the users gmail contact and store into your local database using php codeigniter , mysql , oauth , google api

First, what is a Google API?google_api

The Google API stands for ‘Application Programmable Interface’. As it’s name implies, it is an interface that queries the Google database to help programmers in the development of their applications. At this point, it is important to remember that all of Google’s APIs are only available in beta version, which means they are mostly still in their initial trial release and that there could still be a few adjustments required to some of them.
By definition, Google API’s consist basically of specialized Web services and programs and specialized scripts that enable Internet application developers to better find and process information on the Web. In essence, Google APIs can be used as an added resource in their applications.

How can Google API’s be used effectively?
In the real world, application programmers, developers and integrator write software programs that can connect remotely to the Google API’s. All data communications are executed via the ‘Simple Object Access Protocol’ (SOAP), which is a Web services standards as defined by the industry. The SOAP protocol is an XML-based technology meant to easily exchange information entered into a Web application.
Google’s API can better assist developers in easily accessing Google’s web search database, empowering them in developing software that can query billions of Web documents, constantly refreshed by Google’s automated crawlers. Programmers can initiate search queries to Google’s vast index of more than three billion pages and have results delivered to them as structured data that is simple to analyse and work with.
Additionally, Google API’s can seamlessly access data in the Google cache, while at the same time provide checking in the spelling of words. Google APIs will in fact implement the standardized search syntax used on many of Google’s search properties.

Creating a project in google api console
console
You can use one project to manage all of your applications, or you can create a different project for each one. In deciding whether to create a new project, consider whether you’re collaborating with a different set of people, want to track usage differently, or would set different traffic controls for each application. If so, segregating applications by project in the Console might make sense. You can create as many projects as you need, but please remember that you cannot use multiple projects to try to exceed the limits for API usage by your application.

Here are the general steps for creating a new project:

  1. Create a new project – The dropdown at the top of the page lets you browse, open existing projects, or create a new project. Choose a useful name for the project. Note that the project name is only used in the Console; it is not the same as the project name component used by some Google APIs.
  2. Activate APIs – Go to the All Services pane to activate any APIs that your project will use. Activating an API means that it will shares the same key and billing information as other active APIs in the project, and also adds any API-specific pages to the Console. You can activate the same API in multiple projects with settings unique to each project.
  3. Set up keys and/or OAuth 2.0 credentials – Create the API keys and/or OAuth 2.0 credentials that your client applications will use to identify themselves in API calls.
  4. Copy the client id and client secret , update the return url , where u want to handle the data

NOTE : $Google_redirect_url (redirect_uri) must be ur_domain/ur_controller/google(ur function)
google_code
Create a codeigniter controller with this function and change the first 4 variables

public function google()
{
$Google_api_client_id = “<Enter your data>”;
$Google_client_secret = “<Enter your data>”;
$Google_redirect_url = “<Enter your data>”; // redirect url mentioned in aapi console
$Google_contact_max_result = “<Enter your data>”; // integer value
$authcode= $_GET[“code”];
$clientid=$Google_api_client_id;
$clientsecret=$Google_client_secret
$redirecturi=$Google_redirect_url ;
$fields=array(
‘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,’&’);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,’https://accounts.google.com/o/oauth2/token’);
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//to trust any ssl certificates
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//extracting access_token from response string
$response   =  json_decode($result);
$accesstoken= $response->access_token;
if( $accesstoken!=””)
$_SESSION[‘token’]= $accesstoken;
//passing accesstoken to obtain contact details
$xmlresponse=  file_get_contents(‘https://www.google.com/m8/feeds/contacts/default/full?&max-results=.$Google_contact_max_result .&oauth_token=’.$_SESSION[‘token’]);
//reading xml using SimpleXML
$xml=  new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace(‘gd’, ‘http://schemas.google.com/g/2008’);
$result = $xml->xpath(‘//gd:email’);
$count = 0;
foreach ( $result as $title )
{
$fetched_email = $title->attributes()->address;
$contact_key[] = $this->contact_model->insert_contact_gmail($fetched_email);
}
}

Model :: contact_model

function insert_contact_gmail($contact_email)
{
$this->db->set(‘contact_email’, “$contact_email”);
$this->db->insert(gmail_contact);
}

Table
CREATE TABLE gmail_contact (
‘contact_email’ VARCHAR
);

Call this URL :

https://accounts.google.com/o/oauth2/auth?client_id=<Enter your data>&redirect_uri=<Enter your data>&scope=https://www.google.com/m8/feeds/&response_type=code

3 Comments

  1. If some one wants to be updated with latest technologies
    after that he must be go to see this site and be up to date daily.

  2. Hi there, I discovered your site via Google while looking for a similar matter,
    your site came up, it seems to be good. I have bookmarked it in my google bookmarks.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading