<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class DolarController extends AbstractController
{
private HttpClientInterface $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
#[Route('/api/dolar', name: 'api_dolar')]
public function api(): Response
{
try{
$oficial = $this->client->request('GET', 'https://dolarapi.com/v1/dolares/oficial')->toArray();
$blue = $this->client->request('GET', 'https://dolarapi.com/v1/dolares/blue')->toArray();
} catch (\Exception $e) {
$oficial = ['compra' => 'N/D', 'venta' => 'N/D'];
$blue = ['compra' => 'N/D', 'venta' => 'N/D'];
}
return $this->render('_includes/_headerDolar.html.twig', [
'oficial' => $oficial,
'blue' => $blue,
]);
}
}