Last Updated: February 15, 2018
·
2.92K
· eresendez

Check the exchange rate with the Banxico API.

This is a simple Banxico API call on how to check the exchange rate.

First get a token.

Then select a serie from the catalog about what we want to consult.

Finally:

// See token info at: https://www.banxico.org.mx/SieAPIRest/service/v1/token
$token = 'my_token';

// Get catalog series from https://www.banxico.org.mx/SieAPIRest/service/v1/doc/catalogoSeries#
$catalogs = [
    'SF43787', // Tipo de cambio pesos por dólar E.U.A. Interbancario a 48 horas Apertura compra
    'SF43786', // Tipo de cambio Pesos por dólar E.U.A. Interbancario a 48 horas Cierre venta
];

$series = implode($catalogs, ',');

// Store value in Cache
$query = 'https://www.banxico.org.mx/SieAPIRest/service/v1/series/'.$series.'/datos/oportuno?token='.$token;

return Cache::remember('exchange', 10, function() {
    return json_decode(file_get_contents($query), true);
});

Result:

{
    "bmx": {
        "series": [
            {
                "idSerie": "SF43787",
                "titulo": "Tipo de cambio pesos por dólar E.U.A. Interbancario a 48 horas Apertura compra",
                "datos": [
                    {
                        "fecha": "16/01/2018",
                        "dato": "18.8020"
                    }
                ]
            },
            {
                "idSerie": "SF43786",
                "titulo": "Tipo de cambio Pesos por dólar E.U.A. Interbancario a 48 horas Cierre venta",
                "datos": [
                    {
                        "fecha": "16/01/2018",
                        "dato": "18.7880"
                    }
                ]
            }
        ]
    }
}