PHP code example of andalisolutions / oauth2-anaf

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

    

andalisolutions / oauth2-anaf example snippets


$provider = new Anaf\OAuth2\Client\Provider\AnafProvider(
    clientId: '{anaf-client-id}',
    clientSecret: '{anaf-client-secret}',
    redirectUrl: 'https://example.com/callback-url',
);

// Redirect to the authorization URL
$authorizationUrl = $provider->getAuthorizationUrl();
header('Location: ' . $authorizationUrl);

// This part will be in your callback script
if (isset($_GET['code'])) {
    try {
        $accessToken = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code'],
        ]);
        // We have an access token, which we may use in authenticated
        // requests against the service provider's API.
        echo 'Access Token: ' . $accessToken->getToken() . "<br>";
        echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
        echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
        echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";

    } catch (Exception $e) {
        // Handle errors, such as an invalid code
    }
}