PHP code example of geovanefss / laravel-api-moloni

1. Go to this page and download the library: Download geovanefss/laravel-api-moloni 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/ */

    

geovanefss / laravel-api-moloni example snippets




otenv\Dotenv;
use Geovanefss\LaravelApiMoloni\Exceptions\ApiException;
use Geovanefss\LaravelApiMoloni\Exceptions\TokenException;
use Geovanefss\LaravelApiMoloni\Exceptions\ValidationException;
use Geovanefss\LaravelApiMoloni\Moloni;

try {
    // Load environment variables
    $dotenv = Dotenv::createUnsafeImmutable(__DIR__);
    $dotenv->safeLoad();

    // Set Moloni API configuration using environment variables
    $configs = [
        // Required (see: https://www.moloni.pt/dev/autenticacao/)
        'grant_type' => getenv('MOLONI_GRANT_TYPE'),

        // Required
        'client_id' => getenv('MOLONI_CLIENT_ID'),

        // Required
        'client_secret' => getenv('MOLONI_CLIENT_SECRET'),

        // Required (for authorize)
        'response_type' => getenv('MOLONI_RESPONSE_TYPE'),
        
        // Required (for authorize and grant_type: authorization_code)
        'redirect_uri' => getenv('MOLONI_REDIRECT_URI'),

        // Required (for grant_type: authorization_code)
        'authorization_code' => getenv('MOLONI_AUTHORIZATION_CODE'),

        // Required (for grant_type: password)
        'username' => getenv('MOLONI_USERNAME'),

        // Required (for grant_type: password)
        'password' => getenv('MOLONI_PASSWORD'),

        // Required (for grant_type: refresh_token)
        'refresh_token' => getenv('MOLONI_REFRESH_TOKEN'),
    ];
    
    // Initialize Moloni instance
    $moloni = new Moloni($configs);

    // Start Debug API
    $moloni->startDebug(); // default is to not Debug

    // Stop Debug API
    $moloni->stopDebug(); // default is to not Debug

    // Stop Validate API
    $moloni->stopValidate(); // default is to Validate

    // Start Validate API
    $moloni->startValidate(); // default is to Validate

    // Example: Fetch user profile from Moloni API
    $resp = $moloni->myProfile()->getMe();

    // Output the response in a readable format
    var_dump(json_encode($resp, JSON_PRETTY_PRINT));

} catch (ApiException | ValidationException | TokenException $e) {
    // Handle Moloni API-specific exceptions
    var_dump(get_class($e) . ': ' . $e->toString());
} catch (Exception $e) {
    // Handle general exceptions
    var_dump(get_class($e) . ': ' . $e->getMessage());
}