php curl 获取 邮箱通讯录 gmail

2012-3-24 经测试可正常使用

<?php

error_reporting(E_ALL);

$username = "gmail@gmail.com";
$password = "password";

$gmail = new mail_gmail();
$result = $gmail->getAddressList($username, $password);

$data = json_decode($result, true);
$list = array();
foreach ($data['feed']['entry'] as $entry) {
    $list['username'] = $entry['title']['$t'];
    $list['email']= $entry['gd$email'][0]['address'];
    $lists[] = $list;
}

var_dump($lists);


class mail_gmail {
    function getAddressList($username, $password) {
        $login_url = "https://www.google.com/accounts/ClientLogin";
        $fields = array(
            'Email' => $username,
            '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/$username/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);
        return $result;
    }

}

?>

 

转载于:https://www.cnblogs.com/ybbqg/archive/2012/03/24/2415714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值