PHP code example of beriyack / infomaniak-api-client

1. Go to this page and download the library: Download beriyack/infomaniak-api-client 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/ */

    

beriyack / infomaniak-api-client example snippets


     



pp\InfomaniakApiClient; // Utilisation du namespace correct
use App\Product; // Utilisation du namespace correct

// Remplacez par votre véritable token
// Assurez-vous que API_INFOMANIAK est défini, par exemple dans un fichier config.secret.php
// define('API_INFOMANIAK', 'VOTRE_TOKEN_API_ICI'); 
// Ou chargez-le depuis les variables d'environnement.
if (!defined('API_INFOMANIAK')) {
    die("Le token API Infomaniak n'est pas défini. Veuillez le définir dans config.secret.php ou via une variable d'environnement.");
}

$baseUri = 'https://api.infomaniak.com';
$certificatePath = __DIR__ . '/example/USERTrust RSA Certification Authority.crt'; // Optionnel, pour le développement local

try {
    // 1. Initialisez le client
    $apiClient = new InfomaniakApiClient($baseUri, API_INFOMANIAK, $certificatePath);

    // 2. Récupérez les comptes
    echo "--- Comptes Infomaniak ---\n";
    $accountsResponse = $apiClient->getAccounts();
    if (isset($accountsResponse['result']) && $accountsResponse['result'] === 'success') {
        foreach ($accountsResponse['data'] as $account) {
            echo sprintf("ID: %d, Nom: %s\n", $account['id'], $account['name']);
        }
    } else {
        echo "Impossible de récupérer les comptes.\n";
    }
    echo "\n";

    // 3. Récupérez les produits (les 15 premiers par défaut)
    echo "--- Produits Infomaniak (15 premiers) ---\n";
    $productsResponse = $apiClient->get('/1/products');

    // 4. Traitez les résultats
    if (isset($productsResponse['result']) && $productsResponse['result'] === 'success') {
        // Transformez les données brutes en objets Product
        $products = array_map(fn($p) => new Product($p), $productsResponse['data']);

        foreach ($products as $product) {
            echo sprintf(
                "ID: %d, Nom: %s, Service: %s, Expiration: %s\n",
                $product->getId(),
                $product->getCustomerName(),
                $product->getServiceName(),
                $product->getFormattedExpiredAt()
            );
        }
    } else {
        echo "Impossible de récupérer les produits.\n";
    }

} catch (Exception $e) {
    echo "Une erreur est survenue : " . $e->getMessage();
}

        
        // config.secret.php
        define('API_INFOMANIAK', 'VOTRE_TOKEN_API_ICI');