PHP code example of juniorov / bccr-sdde-exchange-rate

1. Go to this page and download the library: Download juniorov/bccr-sdde-exchange-rate 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/ */

    

juniorov / bccr-sdde-exchange-rate example snippets


   
   r\Sdde\BccrClient;

   $token = getenv('BCCR_SDDE_TOKEN');
   $client = new BccrClient($token);
   $rates = $client->dollarRates();

   echo "Fecha: {$rates->date}\n";
   echo 'Compra: ₡' . number_format($rates->buy->value, 2) . "\n";
   echo 'Venta: ₡' . number_format($rates->sell->value, 2) . "\n";
   

$rates = $client->dollarRates();
// $rates->date       Ejemplo: '2026-07-24'
// $rates->buy->value Ejemplo: 500.25
// $rates->sell->value

$rates = $client->dollarRates(new DateTimeImmutable('2026-07-24'), 7);

$series = $client->series(317, '2026-07-01', '2026-07-24');
foreach ($series as $rate) {
    echo "{$rate->date}: {$rate->value}\n";
}

$client = new BccrClient($token, BccrClient::BASE_URL, 317, 318);
//                                       compra       venta

   'bccr' => ['token' => env('BCCR_SDDE_TOKEN')],
   

   use Bccr\Sdde\BccrClient;

   public function register(): void
   {
       $this->app->singleton(BccrClient::class, fn () => new BccrClient(
           config('services.bccr.token')
       ));
   }
   

   use Bccr\Sdde\BccrClient;
   use Illuminate\Support\Facades\Cache;

   public function show(BccrClient $bccr)
   {
       $rates = Cache::remember('bccr.usd.rates', now()->addHour(),
           fn () => $bccr->dollarRates()
       );
       return response()->json($rates);
   }
   

   define('BCCR_SDDE_TOKEN', 'su_token_real');
   

use Bccr\Sdde\BccrException;

try {
    $rates = $client->dollarRates();
} catch (BccrException $error) {
    if ($error->statusCode === 401) {
        // Token ausente, inválido o vencido.
    } elseif ($error->statusCode === 429) {
        // Demasiadas solicitudes: use caché y reintente más tarde.
    }
    error_log($error->getMessage()); // Nunca registre el token.
}