Skip to content

Commit

Permalink
Added examples and reference to the blog post on how to write extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
Luciano Mammino committed Feb 10, 2014
1 parent eda4f3a commit 003ec4d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ a better understanding of what every method does.

## Examples

TODO
Examples are available in the [examples](examples) directory.

## How to contribute

Expand Down Expand Up @@ -219,8 +219,8 @@ public function __construct()

### A small example

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.
In the meanwhile you can have a look at the [currently existing extractors](src/OAuth/UserData/Extractor/).
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,
step by step, how to write a new extractor.

## Contributors

Expand Down
37 changes: 37 additions & 0 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Bootstrap the library
*/
require_once __DIR__ . '/../vendor/autoload.php';

/**
* Setup error reporting
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);

/**
* Setup the timezone
*/
ini_set('date.timezone', 'Europe/Amsterdam');

/**
* Create a new instance of the URI class with the current URI, stripping the query string
*/
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory();
$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
$currentUri->setQuery('');

/**
* @var array A list of all the credentials to be used by the different services in the examples
*/
$servicesCredentials = array(
'facebook' => array(
'key' => 'put_your_app_key_here',
'secret' => 'put_your_app_secret_here',
)
);

/** @var $serviceFactory \OAuth\ServiceFactory An OAuth service factory. */
$serviceFactory = new \OAuth\ServiceFactory();
54 changes: 54 additions & 0 deletions examples/facebook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* Example of retrieving an authentication token of the Facebook service
* and extract user data. Based on the Lusitanian/PHPoAuthLib facebook example that can be
* found here: https://github.com/Lusitanian/PHPoAuthLib/blob/master/examples/facebook.php
*/

use OAuth\OAuth2\Service\Facebook;
use OAuth\Common\Storage\Session;
use OAuth\Common\Consumer\Credentials;
use OAuth\UserData\ExtractorFactory;

/**
* Bootstrap the example
*/
require_once __DIR__ . '/bootstrap.php';

// Session storage
$storage = new Session();

// Setup the credentials for the requests
$credentials = new Credentials(
$servicesCredentials['facebook']['key'],
$servicesCredentials['facebook']['secret'],
$currentUri->getAbsoluteUri()
);

// Instantiate the Facebook service using the credentials, http client and storage mechanism for the token
/** @var $facebookService Facebook */
$facebookService = $serviceFactory->createService('facebook', $credentials, $storage, array());

if (!empty($_GET['code'])) {
// This was a callback request from facebook, get the token
$token = $facebookService->requestAccessToken($_GET['code']);

// Send a request with it
$result = json_decode($facebookService->request('/me'), true);

// Instantiate the facebook extractor
$extractorFactory = new ExtractorFactory();
$facebookExtractor = $extractorFactory->get($facebookService);

// Show some of the resultant data using the extractor
echo 'Your unique facebook user id is: ' . $facebookExtractor->getUniqueId() .
' and your name is ' . $facebookExtractor->getFullName();

} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
$url = $facebookService->getAuthorizationUri();
header('Location: ' . $url);
} else {
$url = $currentUri->getRelativeUri() . '?go=go';
echo "<a href='$url'>Login with Facebook!</a>";
}

0 comments on commit 003ec4d

Please sign in to comment.