-
-
Save Jamp/897c8e1bca1c0b221f28ad2bfcc69030 to your computer and use it in GitHub Desktop.
Cotizacion de monedas y U.I. desde el BCU (Uruguay)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getCotizaciones($monedas = true) | |
{ | |
$url = 'https://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