PHP code example of asm / eprel-api-client

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

    

asm / eprel-api-client example snippets


use Asm\EprelApiClient\EprelClient;

// Using the Fluent API (Recommended)
$client = (new EprelClient())
    ->uri('https://eprel.ec.europa.eu/api')
    ->apiKey('your-api-key') // If eu/api',
    'cacheTtl' => 3600,
]);

$page = $client->getProducts([
    'productGroup' => 'REFRIGERATORS',
    'energyClass' => 'A',
    'size' => 20,
    'page' => 0
]);

echo "Total Elements found: " . $page->totalElements . "\n";

foreach ($page->content as $productSummary) {
    echo "Brand: " . $productSummary->brandName . "\n";
    echo "Model: " . $productSummary->modelIdentifier . "\n";
    echo "EPREL Reg Number: " . $productSummary->registrationNumber . "\n";
    echo "Energy Class: " . $productSummary->energyClass . "\n";
    echo "---\n";
}

use Asm\EprelApiClient\Exception\ResourceNotFoundException;

try {
    $productDetail = $client->getProduct('123456');
    
    echo "Brand: " . $productDetail->brandName . "\n";
    echo "Label URL: " . $productDetail->energyLabelUrl . "\n";
    echo "Product Sheet URL: " . $productDetail->productInformationSheetUrl . "\n";
    
    // Access arbitrary technical parameters provided by the API
    if (isset($productDetail->technicalParameters['volume'])) {
        echo "Volume: " . $productDetail->technicalParameters['volume'] . " L\n";
    }

} catch (ResourceNotFoundException $e) {
    echo "Product not found in the EPREL database.";
} catch (\Throwable $e) {
    echo "An API error occurred: " . $e->getMessage();
}

use Symfony\Component\Cache\Adapter\RedisAdapter;
use Asm\EprelApiClient\EprelClient;

$redisConnection = RedisAdapter::createConnection('redis://localhost');
$cachePool = new RedisAdapter($redisConnection);

$client = (new EprelClient())
    ->setCache($cachePool)
    ->cacheTtl(86400); // Cache for 24 hours