PHP code example of chillerlan / php-oauth

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

    

chillerlan / php-oauth example snippets


$options = new OAuthOptions;

// set the credentials for your OAuth application
// https://github.com/settings/applications/
$options->key          = '[client_id]';
$options->secret       = '[secret]';
$options->callbackURL  = 'https://example.com/callback/';
// starts the session for the session storage
$options->sessionStart = true;

// a session (or other permanet) storage is 

// display a link to the log-in redirect
echo '<a href="?route=oauth-login">connect with GitHub!</a>';

if($route === 'oauth-login'){
	header('Location: '.$provider->getAuthURL($params, $scopes));

	// -> https://github.com/login/oauth/authorize?client_id=<client_id>
	//       &redirect_uri=https%3A%2F%2Fexample.com%2Fcallback%2F&response_type=code
	//       &scope=<scopes>&state=<state>&type=web_server
}

if($route === 'oauth2-callback' && isset($_GET['code'])){
	$state = null;

	// not all providers support "state"
	if($provider instanceof CSRFToken && isset($_GET['state'])){
		$state = $_GET['state'];
	}

	$token = $provider->getAccessToken($_GET['code'], $state);

	// some providers may send additional data with the callback query parameters,
	// that you may want to save here along with the token

	// when everything is done here, you should redirect the user to wherever they were headed,
	// but also to clear the URL query parameters
	header('Location: ...');
}

if($route === 'oauth1-callback' && isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])){
	$token = $provider->getAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);

	// ...
}

$user = $provider->me(); // -> AuthenticatedUser instance

$request  = $requestFactory->createRequest('GET', 'https://api.github.com/user');
// authenticated request
$response = $provider->sendRequest($request); // -> ResponseInterface instance

composer 
json
{
	": "^8.1",
		"chillerlan/php-oauth": "dev-main#<commit_hash>"
	}
}