Skip to content

Commit 19d320c

Browse files
committed
Add Google+ profile example
1 parent 3ae2049 commit 19d320c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

examples/google_plus_profile.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
require '../src/Curl.class.php';
3+
4+
5+
define('CLIENT_ID', 'XXXXXXXXXXXX.apps.googleusercontent.com');
6+
define('CLIENT_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXXX');
7+
8+
session_start();
9+
10+
if (isset($_GET['code'])) {
11+
$code = $_GET['code'];
12+
13+
// Exchange the authorization code for an access token.
14+
$curl = new Curl();
15+
$curl->post('https://accounts.google.com/o/oauth2/token', array(
16+
'code' => $code,
17+
'client_id' => CLIENT_ID,
18+
'client_secret' => CLIENT_SECRET,
19+
'redirect_uri' => implode('', array(
20+
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http',
21+
'://',
22+
$_SERVER['SERVER_NAME'],
23+
$_SERVER['SCRIPT_NAME'],
24+
)),
25+
'grant_type' => 'authorization_code',
26+
));
27+
28+
if ($curl->error) {
29+
echo $curl->response->error . ': ' . $curl->response->error_description;
30+
exit;
31+
}
32+
33+
$_SESSION['access_token'] = $curl->response->access_token;
34+
header('Location: ?');
35+
} elseif (!empty($_SESSION['access_token']) && !isset($_GET['retry'])) {
36+
// Use the access token to retrieve the profile.
37+
$curl = new Curl();
38+
$curl->setHeader('Content-Type', 'application/json');
39+
$curl->setHeader('Authorization', 'OAuth ' . $_SESSION['access_token']);
40+
$curl->get('https://www.googleapis.com/plus/v1/people/me');
41+
42+
if ($curl->error) {
43+
echo 'Error ' . $curl->response->error->code . ': ' . $curl->response->error->message . '.<br />';
44+
echo '<a href="?retry">Retry?</a>';
45+
exit;
46+
}
47+
48+
echo 'Hi ' . $curl->response->displayName . '.';
49+
} else {
50+
$curl = new Curl();
51+
$curl->get('https://accounts.google.com/o/oauth2/auth', array(
52+
'scope' => 'https://www.googleapis.com/auth/plus.me',
53+
'redirect_uri' => implode('', array(
54+
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http',
55+
'://',
56+
$_SERVER['SERVER_NAME'],
57+
$_SERVER['SCRIPT_NAME'],
58+
)),
59+
'response_type' => 'code',
60+
'client_id' => CLIENT_ID,
61+
'approval_prompt' => 'force',
62+
));
63+
64+
$url = $curl->response_headers['Location'];
65+
echo '<a href="' . $url . '">Continue</a>';
66+
}

0 commit comments

Comments
 (0)