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
$client = new apiClient();
$client->setApplicationName('Engir');
$client->setScopes("http://www.google.com/m8/feeds/");
$auth = $client->createAuthUrl();
1. https://developers.google.com/google-apps/contacts/v3/
2. http://lookmywebpage.com/api/google/import-gmail-or-google-contacts-using-php-and-oauth-2-0/