Get currency exchange rate from Yahoo server in PHP

/*
 * Get currency exchange rate from Yahoo server.
 *
 * @return float rate on success, else 0
 */
function get_exchange_rate( $currency_from, $currency_to ) {
	$url = "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D" . $currency_from . $currency_to . "%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json";
	$exchange_rate = json_decode( file_get_contents( $url ) );
	return ( isset( $exchange_rate->query->results->row->rate ) ) ? floatval( $exchange_rate->query->results->row->rate ) : 0;
}

No comments yet.

Leave a Reply