Function:
function exchangeRate( $amount, $currency, $exchangeIn )
{
$googleQuery = $amount . ' ' . $currency . ' in ' . $exchangeIn;
$googleQuery = urlEncode( $googleQuery );
$askGoogle = file_get_contents( 'http://www.google.com/search?q=' . $googleQuery );
$askGoogle = strip_tags( $askGoogle );
$matches = array();
preg_match( '/= (([0-9]|\.|,|\ )*)/', $askGoogle, $matches );
return $matches[1] ? $matches[1] : false;
}
Usage:
echo exchangeRate( 1 , 'USD' , 'EURO' );
Or you can also use this function from oscommerce, rate is gather from www.xe.com
Function:
function quote_xe_currency($to, $from , $amount) {
$page = file('http://www.xe.net/ucc/convert.cgi?Amount='.$amount.'&From=' . $from . '&To=' . $to);
$match = array();
preg_match('/[0-9.]+\s*' . $from . '\s*=\s*([0-9.]+)\s*' . $to . '/', implode('', $page), $match);
if (sizeof($match) > 0) {
return $match[1];
} else {
return false;
}
}
Usage:
echo quote_xe_currency("USD","EURO","1");
smile!
No comments:
Post a Comment