Saturday 20 September 2014

Add Contacts to Gamil using PHP Curl


Hi Friends I am previously Explan about how to getting google(Gmail) contacts using Google API with php, you can find the process of getting gamil contacts Article here Getting Google Contacts
Now explain about how to add contacts to our gmail account it is very is process through PHP Curl in this process we need "access_token". the following snippet of code is used for add contacts to my gmail account.


<?php
   session_start();
   $access_token ='your Access token';
    
   $contactXML = '<?xml version="1.0" encoding="utf-8"?>
 <atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
 <atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
    <gd:name>
   <gd:givenName>srinu</gd:givenName>
   <gd:fullName>srinu chilukuri</gd:fullName>
   <gd:familyName>chilukuri</gd:familyName>
    </gd:name>
 <gd:email rel="http://schemas.google.com/g/2005#home" address="'.$email.'"/>
    <gd:im address="knowledgecorner.srinu@gmail.com"
    protocol="http://schemas.google.com/g/2005#GOOGLE_TALK"
    primary="true"
    rel="http://schemas.google.com/g/2005#home"/>
 <gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">9999999999</gd:phoneNumber>
  </atom:entry>';

   $headers = array(
   'Host: www.google.com',
   'Gdata-version: 3.0',
   'Content-length: '.strlen($contactXML),
   'Content-type: application/atom+xml',
   'Authorization: OAuth '.$access_token
   );

    $contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/';
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $contactQuery );
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
   curl_setopt($ch, CURLOPT_TIMEOUT, 400);
   curl_setopt($ch, CURLOPT_FAILONERROR, true);
   curl_exec($ch);
  ?>

No comments:

Post a Comment