20121009

Update daily USD to EUR exchange rate in PHP using openexchangerates.org

The process, loops for the last days and call the API of openexchangerates.org, storing in the database the daily conversion from USD to EUR.

require_once('./conf/config.php');
require_once('./models/exchanges.php');

$exchanges=new exchanges();
$days = array();
for($i = 1; $i < 10; $i++)
{
  $days[] = date("Y-m-d", strtotime('-'. $i .' days'));
}
foreach ($days as $day)
{
  $json = file_get_contents('http://openexchangerates.org/api/historical/'.$day.'.json?app_id=YOURAPIID');
  $data = json_decode($json, TRUE);
  $conversion=$data['rates']['EUR'];
 if ($exchanges->exists($day))
 {
   $ret=$exchanges->update($day, $conversion);
 }
 else
 {
   $ret=$exchanges->insert($day, $conversion);
 }
  echo($day.' '.$conversion.' '.$ret).SEP;
}


Enjoy!!!

No hay comentarios: