php获取谷歌邮箱,php 获取gmail 联系人邮箱,应用oauth2.0验证

1.  创建应用

https://code.google.com/apis/console/

设置相应的 应用名 转向URL地址

API Access下会得到

oauth2_client_id , oauth2_client_secret , oauth2_redirect_uri

2.  下载google 客户端工具 for php

https://developers.google.com/google-apps/tasks/downloads?hl=zh-CN

3.  然后设置 config.php

主要是配置

'oauth2_client_id' => '6644545content.com',

'oauth2_client_secret' => 'ZJcFnO45452sB6hZGKCp3',

'oauth2_redirect_uri' => 'http://e454545com/login/gmail/',

4.  程序代码如下

因为用google提供的例子只能获得到邮箱的用户名,得不到email地址,所以需要分两次取,然后再合并。

require_once ('BaseAction.php');

require_once ("../lib/google_lib/apiClient.php");

$client = new apiClient();

$client->setApplicationName('Engir');

$client->setScopes("http://www.google.com/m8/feeds/");

// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html

// Visit https://code.google.com/apis/console?api=contacts to generate your

// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.

// $client->setClientId('insert_your_oauth2_client_id');

// $client->setClientSecret('insert_your_oauth2_client_secret');

// $client->setRedirectUri('insert_your_redirect_uri');

// $client->setDeveloperKey('insert_your_developer_key');

if (isset($_GET['code'])) {

$client->authenticate();

$_SESSION['token'] = $client->getAccessToken();

$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

}

if (isset($_SESSION['token'])) {

$client->setAccessToken($_SESSION['token']);

}

if (isset($_REQUEST['logout'])) {

unset($_SESSION['token']);

$client->revokeToken();

header("location:".Config::ROOT_URL."/invites/gmail/");

}

if ($client->getAccessToken()) {

$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full?max-results=100");

$val = $client->getIo()->authenticatedRequest($req);

// The contacts api only returns XML responses.

$response = json_encode(simplexml_load_string($val->getResponseBody()));

$nameArray = array();

$responseArray = json_decode($response, true);

if (isset($responseArray["entry"])){

$entry = $responseArray["entry"];

foreach ($entry as $item){

if(!is_array($item["title"])){

array_push($nameArray,$item["title"]);

}else {

array_push($nameArray,"##");

}

}

}

// The access token may have been updated lazily.

$_SESSION['token'] = $client->getAccessToken();

//print "

" . print_r(json_decode($response, true), true) . "
";

//reading xml using SimpleXML

$emailArray = array();

$xml= new SimpleXMLElement($val->getResponseBody());

$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');

$result = $xml->xpath('//gd:email');

foreach ($result as $title) {

$email = $title->attributes()->address;

if (!empty($email)){

array_push($emailArray,$email."");

}

}

$userArray = array();

for ($i=0;$i

$user = array();

$name = $nameArray[$i];

$email = $emailArray[$i];

if ($name == "##"){

$name = strstr($email, '@', true);

}

$user["name"] = $name;

$user["email"] = $email;

array_push($userArray,$user);

}

验证请求的URL地址是这样产生的

$client = new apiClient();

$client->setApplicationName('Engir');

$client->setScopes("http://www.google.com/m8/feeds/");

$auth = $client->createAuthUrl();

参考URL:

1. https://developers.google.com/google-apps/contacts/v3/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值