Simple, yet effective way to get currency rate from yahoo finance service. Cache the results, or you will get ban from yahoo.
function get_currency_rate($from, $to){
$json = get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22".$from.$to."%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$json = json_decode($json);
$rate = $json->query->results->rate->Rate;
if($rate < 0.001){
// try again reversal
$json = get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22".$to.$from."%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$json = json_decode($json);
$rate = $json->query->results->rate->Rate;
if($rate != 0.00){
$rate = 1/$rate;
}
}
return $rate;
}
No comments:
Post a Comment
You can ask IT questions in comments!