PHP code example of helloasso / helloasso-php

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

    

helloasso / helloasso-php example snippets





 Configure OAuth2 access token for authorization: OAuth2
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');


$apiInstance = new OpenAPI\Client\Api\AnnuaireApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);
$page_size = 20; // int | The number of items per page
$continuation_token = 'continuation_token_example'; // string | Continuation Token from which we wish to retrieve results
$hello_asso_api_v5_models_directory_list_forms_request = new \OpenAPI\Client\Model\HelloAssoApiV5ModelsDirectoryListFormsRequest(); // \OpenAPI\Client\Model\HelloAssoApiV5ModelsDirectoryListFormsRequest | Body which contains the filters to apply

try {
    $result = $apiInstance->directoryFormsPost($page_size, $continuation_token, $hello_asso_api_v5_models_directory_list_forms_request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling AnnuaireApi->directoryFormsPost: ', $e->getMessage(), PHP_EOL;
}




use League\OAuth2\Client\Provider\GenericProvider;

$provider = new GenericProvider([
    'clientId'                => 'your_client_id',
    'clientSecret'            => 'your_client_secret',
    'urlAccessToken'          => 'https://api.helloasso.com/oauth2/token',
    'urlAuthorize'            => '',
    'urlResourceOwnerDetails' => ''
]);

try {
    // Get access token using client credentials
    $accessToken = $provider->getAccessToken('client_credentials');

    echo 'Access Token: ' . $accessToken->getToken();
    echo 'Expires in: ' . $accessToken->getExpires();
    echo 'Refresh Token: ' . $accessToken->getRefreshToken();
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
    echo 'Error: ' . $e->getMessage();
}



use League\OAuth2\Client\Provider\GenericProvider;

$provider = new GenericProvider([
    'clientId'                => 'your_client_id',
    'clientSecret'            => 'your_client_secret',
    'urlAccessToken'          => 'https://api.helloasso.com/oauth2/token',
    'urlAuthorize'            => '',
    'urlResourceOwnerDetails' => ''
]);

$refreshToken = 'your_refresh_token';

try {
    // Refresh the access token
    $newAccessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $refreshToken,
    ]);

    echo 'New Access Token: ' . $newAccessToken->getToken();
    echo 'Expires in: ' . $newAccessToken->getExpires();
    echo 'New Refresh Token: ' . $accessToken->getRefreshToken();
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
    echo 'Error: ' . $e->getMessage();
}



use League\OAuth2\Client\Provider\GenericProvider;

$provider = new GenericProvider([
    'clientId'                => 'your_client_id',
    'clientSecret'            => 'your_client_secret',
    'redirectUri'             => 'https://your-app.com/callback',
    'urlAuthorize'            => 'https://auth.helloasso.com/authorize',
    'urlAccessToken'          => 'https://api.helloasso.com/oauth2/token',
    'urlResourceOwnerDetails' => ''
]);

// Function to generate a random code verifier
function generateCodeVerifier() {
    return bin2hex(random_bytes(32));
}

// Function to generate a code challenge from the code verifier
function generateCodeChallenge($codeVerifier) {
    return rtrim(strtr(base64_encode(hash('sha256', $codeVerifier, true)), '+/', '-_'), '=');
}

// Step 1: Generate the authorization URL
function GetAuthorizationUrl($provider) {
    $codeVerifier = generateCodeVerifier();
    $codeChallenge = generateCodeChallenge($codeVerifier);

    $authorizationUrl = $provider->getAuthorizationUrl([
        'state' => 'random_state_string',
        'code_challenge' => $codeChallenge,
        'code_challenge_method' => 'S256',
    ]);

    echo 'Authorization URL: ' . $authorizationUrl . "\n";
    echo 'Code verifier: ' . $codeVerifier . "\n";
}

// Step 2: Handle the callback and exchange the authorization code for an access token
function GetAccessTokenFromCode($provider, $authorizationCode, $codeVerifier) {
    try {
        // Get access token
        $accessToken = $provider->getAccessToken('authorization_code', [
            'code' => $authorizationCode,
            'code_verifier' => $codeVerifier
        ]);

        echo 'Access Token: ' . $accessToken->getToken() . "\n";
        echo 'Expires in: ' . $accessToken->getExpires() . "\n";
        echo 'New Refresh Token: ' . $accessToken->getRefreshToken() . "\n";
    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
        echo 'Error: ' . $e->getMessage();
    }
}

GetAuthorizationUrl($provider);
// After user authorizes, exchange the code (passed in the redirect URL callback)
//GetAccessTokenFromCode($provider, 'your_authorization_code', 'your_code_verifier');

composer