PHP code example of spoonwep / oauth2-weibo

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

    

spoonwep / oauth2-weibo example snippets


session_start();
$provider = new \spoonwep\OAuth2\Client\Provider\Weibo([
	'clientId' => '{weibo App Key}',
	'clientSecret' => '{weibo App Secret}',
	'redirectUri' => '{http://example.com/callback-url}',
]);
if (!isset($_GET['code'])) {
	$authUrl = $provider->getAuthorizationUrl();
	$_SESSION['oauth2state'] = $provider->getState();
	header('Location: '.$authUrl);
	exit;
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
	unset($_SESSION['oauth2state']);
	exit('Invalid state');
} else {
	$token = $provider->getAccessToken('authorization_code', [
		'code' => $_GET['code']
	]);

	//fetch userinfo returned by serverside
	$user = $provider->getResourceOwner($token);
	print_r($user->toArray());
}