PHP code example of maye / oauth-client

1. Go to this page and download the library: Download maye/oauth-client 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/ */

    

maye / oauth-client example snippets


use Maye\OAuthClient;

define('CONSUMER_KEY', 'your twitter consumer key');
define('CONSUMER_SECRET', 'your twitter consumer secret');

$client = new OAuth1Client(
	'twitter', 
	CONSUMER_KEY, 
	CONSUMER_SECRET, 
	'/oauth/twitter/callback'
);

// redirect
$client->authorize();
exit;

$denied = $_GET['denied'];
$oauthToken = $_GET['oauth_token'];
$oauthVerifier = $_GET['oauth_verifier'];

if (!empty($denied)) {
	// should be handled as error
	echo 'error';
	exit;
}

/** @var OAuth\OAuth1\Token\TokenInterface $token */
$token = $client->requestAccessToken($oauthToken, $oauthVerifier);

$accessToken       = $token->getAccessToken();
$accessTokenSecret = $token->getAccessTokenSecret();
$userId     = $token->getExtraParams()['user_id'];
$screenName = $token->getExtraParams()['screen_name'];

// gets the authorized user info
$result = $client->api('get', 'users/show.json', ['user_id' => $userId]);
$result = json_decode($result);

$name = $result->name;

use Maye\OAuthClient;
use OAuth\OAuth2\Service\Facebook;

define('CONSUMER_KEY', 'your facebook consumer key');
define('CONSUMER_SECRET', 'your facebook consumer secret');

$client = new OAuth2Client(
	'facebook',
	CONSUMER_KEY, 
	CONSUMER_SECRET, 
	'/oauth/facebook/callback',
	[Facebook::SCOPE_READ_STREAM, Facebook::SCOPE_PUBLISH_ACTIONS]
);

// redirect
$client->authorize();
exit;

$code = $_GET['code'];

if (empty($code)) {
	// should be handled as error
	echo 'error';
	exit;
}

/** @var OAuth\OAuth2\Token\TokenInterface $token */
$token = $client->requestAccessToken($code);

$accessToken  = $token->getAccessToken();
$refreshToken = $token->getRefreshToken();

// gets the authorized user info
$result = $client->api('get', '/me');
$result = json_decode($result);

$id = $result->id;
$name = $result->name;