Search code examples
phpoauth-2.0google-oauthgoogle-api-php-clientgoogle-contacts-api

Editing google contacts and adding new google contacts through codeigniter application


Good Afternoon, I have just created a page which can import google contacts and display them using OAuth2. I have used the google-api-php-client for this. I can successfully get all the contacts' details and display them. However, my main goal is to be able to edit the google contacts' details and add new google contacts from my codeigniter application based on particular user actions. I want to know what functions I need to write and what to do in the corresponding views. Please help me. Thank you very much in advance.

Code:

<?php 
error_reporting(E_ALL);
ini_set("display_errors", 1);

session_start(); ?>
<!DOCTYPE html>
<html class="no-js" lang="en"/>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Google Contacts API</title>
</head>

<body>
<h2>Google Contacts API v3.0</h2>
<?php
require_once 'lib/google-api-client/autoload.php';
require 'lib/google-api-client/Config.php';
require 'lib/google-api-client/Google_Client.php';

$client_id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbb.cccccccccccc.com';
$client_secret = 'e3fsfds4gfg23ha93kmKFkfgK';
$redirect_uri = 'http://ccccccccccccccccccc.com/rddddddddddd/index.php';

$client = new Google_Client();
$client -> setApplicationName('contact');
$client -> setClientid($client_id);
$client -> setClientSecret($client_secret);
$client -> setScopes('https://www.google.com/m8/feeds');
$client -> setRedirectUri($redirect_uri);
$client -> setAccessType('online');

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    header('Location: ' . $redirect_uri);
}

if(!isset($_SESSION['token']))
{
    $url = $client->createAuthUrl($_SESSION['token']);
    echo '<a href="' . $url . '">Import Google Contacts</a>';
}else{
        $client->setAccessToken($_SESSION['token']);
        $token = json_decode($_SESSION['token']);
        $token->access_token;
        $curl = curl_init("https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=1000&access_token=" . $token->access_token);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        $contacts_json = curl_exec($curl);
        curl_close($curl);
        $contacts = json_decode($contacts_json, true);
        $return = array();
        foreach($contacts['feed']['entry'] as $contact){
            $return[] = array(
            'name' => $contact['title']['$t'],
            'email' => isset($contact['gd$email'][0]['address']) ? $contact['gd$email'][0]['address'] : false,
            'phone' => isset($contact['gd$phoneNumber'][0]['$t']) ? $contact['gd$phoneNumber'][0]['$t'] :false,
            );
        }
        echo "<pre>";
        var_dump($return);
        echo "</pre>";
    }       
?>

</body>
</html>

Please feel free to ask for more details.


Solution

  • To update a contact, first retrieve the contact entry (which you have already done), modify the data and send an authorized PUT request to the contact's edit URL with the modified contact entry in the body.

    URL: https://www.google.com/m8/feeds/contacts/userEmail/full/{contactId}

    To create a new contact, send an authorized POST request to the user's contacts feed URL with contact data in the body.

    URL: https://www.google.com/m8/feeds/contacts/{userEmail}/full