/
google_connected.php
55 lines (55 loc) · 1.8 KB
/
google_connected.php
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
<?php
session_start();
require_once('config.php');
require_once ('social_library/google/Google_Client.php');
$client = new Google_Client();
$client->setApplicationName('Google Contacts PHP Sample');
$client->setScopes('http://www.google.com/m8/feeds/');
$client->setClientId($google_client_id);
$client->setClientSecret($google_client_secret);
$client->setRedirectUri($google_redirect_url);
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();
}
if ($client->getAccessToken())
{
$req = new Google_HttpRequest('https://www.google.com/m8/feeds/contacts/default/thin?q=email&max-results=500');
$val = $client->getIo()->authenticatedRequest($req);
$response = json_encode(simplexml_load_string($val->getResponseBody()));
$_SESSION['token'] = $client->getAccessToken();
$xml = simplexml_load_string($val->getResponseBody());
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$user_contacts = array();
foreach ($xml->entry as $entry)
{
foreach ($entry->xpath('gd:email') as $email)
{
$user_contacts[] = array('title'=>(string)$entry->title, 'email'=>(string)$email->attributes()->address);
}
}
?>
<script type='text/javascript'>
var emails = '<?php echo json_encode($user_contacts, JSON_FORCE_OBJECT); ?>';
window.opener.GetValueFromChild(emails);
window.close();
</script>
<?php }
else
{
//$auth = $client->createAuthUrl();
header('Location: '.$client->createAuthUrl());
exit;
}