Skip to content

Instantly share code, notes, and snippets.

@jtechera
Last active February 13, 2019 00:41
Show Gist options
  • Save jtechera/2ec0f88acf5e358d93472f9592ad3884 to your computer and use it in GitHub Desktop.
Save jtechera/2ec0f88acf5e358d93472f9592ad3884 to your computer and use it in GitHub Desktop.
Cotizacion de monedas y U.I. desde el BCU (Uruguay)
<?php
function getCotizaciones($monedas = true)
{
$url = 'http://www.bcu.gub.uy/_layouts/BCU.Cotizaciones/handler/CotizacionesHandler.ashx?op=getcotizaciones';
$data = "";
$last_date_found = false;
$diff = 1;
$cotizaciones = array();
$codigosAceptados = array("USD", "EURO", "CHF", "GBP", "ARS", "BRL", "JPY", "U.I.");
while (!$last_date_found) {
$fecha = date("d") - $diff . "/" . date("m") . "/" . date("Y");
if ($monedas) {
$data = '{"KeyValuePairs":{"Monedas":[{"Val":"500","Text":"PESO ARGENTINO"},{"Val":"1000","Text":"REAL"},{"Val":"1111","Text":"EURO"},{"Val":"2222","Text":"DOLAR USA"},{"Val":"2700","Text":"LIBRA ESTERLINA"},{"Val":"3600","Text":"YEN"},{"Val":"5900","Text":"FRANCO SUIZO"}],"FechaDesde":"' . $fecha . '","FechaHasta":"' . $fecha . '","Grupo":"1"}}';
} else {
$data = '{"KeyValuePairs":{"Monedas":[{"Val":"9800","Text":"UNIDAD INDEXADA"}],"FechaDesde":"' . $fecha . '","FechaHasta":"' . $fecha . '","Grupo":"2"}}';
}
$options = array(
'http' => array(
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => $data
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result !== FALSE) {
$result = json_decode($result);
if ($result->cotizacionesoutlist->RespuestaStatus->status === 0) {
$diff++;
} else {
$last_date_found = true;
foreach ($result->cotizacionesoutlist->Cotizaciones as $cotizacion) {
if (in_array($cotizacion->CodigoISO, $codigosAceptados)) {
array_push($cotizaciones, $cotizacion);
}
}
}
}
}
return ($cotizaciones);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment