PHP code example of vdmi / guzzle-oauth

1. Go to this page and download the library: Download vdmi/guzzle-oauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vdmi / guzzle-oauth example snippets




// Authorize flow, send user to provider to authorize
$config = array(
  'consumer_key' => '--YOUR-APP-ID-FROM-FACEBOOK--',
  'consumer_secret' => '--YOUR-APP-SECRET-FROM-FACEBOOK--',
  'scope' => 'email,manage_pages', // scopes for this connection
);
$provider = 'facebook';
$client = \GuzzleOauth\Consumers::get($provider, $config);

// return path after authorization.
$callback_uri = 'http://test.com/callback/uri';

// state param, that makes the round trip to facebook and must be the same on return.
$state = '--some-random-string--'; //optional

// oAuth2 does not use request tokens, we use it here to be consistent with oAuth1
// This makes it possible to replace 'facebook' with 'twitter' in $provider without
// changing the code.
$request_token = $client->getRequestToken($callback_uri);

// get the redirect url
$url = $client->getAuthorizeUrl($request_token, $callback_uri, $state);

// Note!
// You need to store $request_token (and $state) in a session or db, so that you can
// pickit up on return.
$_SESSION['REQUEST_TOKEN_' . $provider] = serialize($request_token);

// send the user to facebook to login and authorize your app.
header('Location: ' . $url);
exit;

// Return (callback) after authorize
// In our example we are on $callback_uri (http://test.com/callback/uri)

// We setup our client again
$config = array(
  'consumer_key' => '--YOUR-APP-ID-FROM-FACEBOOK--',
  'consumer_secret' => '--YOUR-APP-SECRET-FROM-FACEBOOK--',
  'scope' => 'email,manage_pages', // scopes for this connection
);
$provider = 'facebook';
$client = \GuzzleOauth\Consumers::get($provider, $config);

// Let's get the request token
$request_token = unserialize($_SESSION['REQUEST_TOKEN_' . $provider]);

// we could test if $_GET['state'] is the same as the $state from above.

// Let us get the access_token
$access_token = $client->getAccessToken($_GET, $request_token);

// Store access token in session, unset request token
unset($_SESSION['REQUEST_TOKEN_' . $provider]);
$_SESSION['ACCESS_TOKEN_' . $provider] = serialize($access_token);

// Now that we have an access token, do calls to facebook
$provider = 'facebook';

// Get access token
$access_token = unserialize($_SESSION['ACCESS_TOKEN_' . $provider]);

// Add access token to config
$config = array(
  'consumer_key' => '--YOUR-APP-ID-FROM-FACEBOOK--',
  'consumer_secret' => '--YOUR-APP-SECRET-FROM-FACEBOOK--',
  'scope' => 'email,manage_pages', // scopes for this connection
) + $access_token;

// create client
$client = \GuzzleOauth\Consumers::get($provider, $config);

// Get a collection with all user info
$info = $client->getUserInfo();

// Get remote user id without making a new http call
$facebook_id = $client->getUserId($info);

// Get remote user id making a new http call
$facebook_id = $client->getUserId();

// Get email (if provider provides one (note FB needs the scope 'email'))
$email = $client->getUserEmail($info);

// Get user account label (name)
$label = $client->getUserLabel($info);

// $client is a normal guzzle client, so we can talk to any endpoint.
$data = $client->get('me/likes')->send()->json();
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php