• Import Gmail contacts using PHP

    In this tutorial, we can import Gmail contacts using PHP.

    Kindly follow below steps.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    
     
    <?php
     
     error_reporting(E_ALL);
     
     $user = "Your Gmail ID";  // Enter your  gmail ID
     
    $password = "Your Gmail Password"; // Enter your Gmail account password.
     
     
     
    // ref: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
     
     
     
    // step 1: login
     
    $login_url = "https://www.google.com/accounts/ClientLogin";
     
    $fields = array(
     
                    'Email' => $user,
     
                    'Passwd' => $password,
     
                    'service' => 'cp', // <== contact list service code
     
                    'source' => 'test-google-contact-grabber',
     
                    'accountType' => 'GOOGLE',
     
    );
     
     
     
    $curl = curl_init();
     
    curl_setopt($curl, CURLOPT_URL,$login_url);
     
    curl_setopt($curl, CURLOPT_POST, 1);
     
    curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
     
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     
    $result = curl_exec($curl);
     
     
     
    $returns = array();
     
     
     
    foreach (explode("\n",$result) as $line)
     
    {
     
                    $line = trim($line);
     
                    if (!$line) continue;
     
                    list($k,$v) = explode("=",$line,2);
     
     
     
                    $returns[$k] = $v;
     
    }
     
     
     
    curl_close($curl);
     
     
     
    // step 2: grab the contact list
     
    $feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";
     
     
     
    $header = array(
     
                    'Authorization: GoogleLogin auth=' . $returns['Auth'],
     
    );
     
     
     
    $curl = curl_init();
     
    curl_setopt($curl, CURLOPT_URL, $feed_url);
     
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
     
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     
     
     
    $result = curl_exec($curl);
     
    curl_close($curl);
     
     
     
    $data = json_decode($result);
     
     
     
    $contacts = array();
     
     
     
    foreach ($data->feed->entry as $entry)
     
    {
     
                    $contact = new stdClass();
     
                    $contact->title = $entry->title->{'$t'};
     
                    $contact->email = $entry->{'gd$email'}[0]->address;
     
     
     
                    $contacts[] = $contact;
     
    }
     
     
     
    echo "<pre>";
     
    print_r($contacts);

Comments on this post

Leave a Reply

  • Security Code :


    six − 4 =