Skip to content

Commit 003ec4d

Browse files
author
Luciano Mammino
committed
Added examples and reference to the blog post on how to write extractors
1 parent eda4f3a commit 003ec4d

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ a better understanding of what every method does.
162162

163163
## Examples
164164

165-
TODO
165+
Examples are available in the [examples](examples) directory.
166166

167167
## How to contribute
168168

@@ -219,8 +219,8 @@ public function __construct()
219219

220220
### A small example
221221

222-
I hope to have time to write a dedicated blog post to present a small example that explains, step by step, how to write a new extractor.
223-
In the meanwhile you can have a look at the [currently existing extractors](src/OAuth/UserData/Extractor/).
222+
I wrote [a dedicated blog post](http://loige.com/writing-a-new-extractor-for-php-oauth-user-data/) to present a small example that explains,
223+
step by step, how to write a new extractor.
224224

225225
## Contributors
226226

examples/bootstrap.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Bootstrap the library
5+
*/
6+
require_once __DIR__ . '/../vendor/autoload.php';
7+
8+
/**
9+
* Setup error reporting
10+
*/
11+
error_reporting(E_ALL);
12+
ini_set('display_errors', 1);
13+
14+
/**
15+
* Setup the timezone
16+
*/
17+
ini_set('date.timezone', 'Europe/Amsterdam');
18+
19+
/**
20+
* Create a new instance of the URI class with the current URI, stripping the query string
21+
*/
22+
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory();
23+
$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
24+
$currentUri->setQuery('');
25+
26+
/**
27+
* @var array A list of all the credentials to be used by the different services in the examples
28+
*/
29+
$servicesCredentials = array(
30+
'facebook' => array(
31+
'key' => 'put_your_app_key_here',
32+
'secret' => 'put_your_app_secret_here',
33+
)
34+
);
35+
36+
/** @var $serviceFactory \OAuth\ServiceFactory An OAuth service factory. */
37+
$serviceFactory = new \OAuth\ServiceFactory();

examples/facebook.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Example of retrieving an authentication token of the Facebook service
5+
* and extract user data. Based on the Lusitanian/PHPoAuthLib facebook example that can be
6+
* found here: https://github.com/Lusitanian/PHPoAuthLib/blob/master/examples/facebook.php
7+
*/
8+
9+
use OAuth\OAuth2\Service\Facebook;
10+
use OAuth\Common\Storage\Session;
11+
use OAuth\Common\Consumer\Credentials;
12+
use OAuth\UserData\ExtractorFactory;
13+
14+
/**
15+
* Bootstrap the example
16+
*/
17+
require_once __DIR__ . '/bootstrap.php';
18+
19+
// Session storage
20+
$storage = new Session();
21+
22+
// Setup the credentials for the requests
23+
$credentials = new Credentials(
24+
$servicesCredentials['facebook']['key'],
25+
$servicesCredentials['facebook']['secret'],
26+
$currentUri->getAbsoluteUri()
27+
);
28+
29+
// Instantiate the Facebook service using the credentials, http client and storage mechanism for the token
30+
/** @var $facebookService Facebook */
31+
$facebookService = $serviceFactory->createService('facebook', $credentials, $storage, array());
32+
33+
if (!empty($_GET['code'])) {
34+
// This was a callback request from facebook, get the token
35+
$token = $facebookService->requestAccessToken($_GET['code']);
36+
37+
// Send a request with it
38+
$result = json_decode($facebookService->request('/me'), true);
39+
40+
// Instantiate the facebook extractor
41+
$extractorFactory = new ExtractorFactory();
42+
$facebookExtractor = $extractorFactory->get($facebookService);
43+
44+
// Show some of the resultant data using the extractor
45+
echo 'Your unique facebook user id is: ' . $facebookExtractor->getUniqueId() .
46+
' and your name is ' . $facebookExtractor->getFullName();
47+
48+
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
49+
$url = $facebookService->getAuthorizationUri();
50+
header('Location: ' . $url);
51+
} else {
52+
$url = $currentUri->getRelativeUri() . '?go=go';
53+
echo "<a href='$url'>Login with Facebook!</a>";
54+
}

0 commit comments

Comments
 (0)