Using Yahoo! Finance for Currency Exchange Rate Retrieval
This is an old revision of the document!
In a bid to find a more reliable source for currency exchange rates and faster servers to boot, I’ve created this Module to get rates from Yahoo! Finance. Note that this may be subject to Yahoo! T’s & C’s so you better read them before using this.
I’ve made this upgrade-proof to the best of my knowledge; it uses local xml and Module code. But of course, do take back-ups and test your back-ups. This is supplied as is and is not implied to work at all. You know the drill.
Step 1 - Create Folders & File Structure |
I have mimicked the Webservicex currency retrieval code. That one resides in /app/code/core/Mage/Directory/Model/Currency/Import/Webservicex.php so you can have a look yourself. In line with that code and structure, first create the following folder structure and file:
/app/code/local/JT/Directory/Model/Currency/Import/Yahoofinance.php
Step 2 - Paste Code into Yahoofinance.php |
- <?php
- /**
- * JT
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Open Software License (OSL 3.0)
- * that is bundled with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://opensource.org/licenses/osl-3.0.php
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@magentocommerce.com so we can send you a copy immediately.
- *
- * @category JT
- * @package JT_Directory
- * @copyright Copyright (c) 2009 JT
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- *
- * All credits to Magento Core Development Team who's Webservicex code I liberally borrowed
- * as well as Mark Alexander Bain's idea and code as displayed on the following url, accessed Feb 16th 2009
- * http://ajax-programming.suite101.com/article.cfm/currency_conversion_with_yahoo_finance
- */
- /**
- * Currency rate import model (From quote.yahoo.com)
- *
- * @category JT
- * @package JT_Directory
- * @author Magento Enthusiast <J.T.>
- */
- class JT_Directory_Model_Currency_Import_Yahoofinance extends Mage_Directory_Model_Currency_Import_Abstract
- {
- protected $_url = 'http://quote.yahoo.com/d/quotes.csv?s={{CURRENCY_FROM}}{{CURRENCY_TO}}=X&f=l1&e=.csv';
- protected $_messages = array();
- protected function _convert($currencyFrom, $currencyTo, $retry=0)
- {
- $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url);
- $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);
- try {
- sleep(1); //Be nice to Yahoo, they don't have a lot of hi-spec servers
- $handle = fopen($url, "r");
- $exchange_rate = fread($handle, 2000);
- fclose($handle);
- if( !$exchange_rate ) {
- $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
- return null;
- }
- return (float) $exchange_rate * 1.0; // change 1.0 to influence rate;
- }
- catch (Exception $e) {
- if( $retry == 0 ) {
- $this->_convert($currencyFrom, $currencyTo, 1);
- } else {
- $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
- }
- }
- }
- }
Step 3 - Amend local.xml |
That’s /app/etc/local.xml - add this after the <global> tag:
- <!-- JT Yahoo Finance Integration-->
- <currency>
- <import>
- <services>
- <yahoofinance>
- <name>Yahoo Finance</name>
- <model>JT_Directory_Model_Currency_Import_Yahoofinance</model>
- </yahoofinance>
- </services>
- </import>
- </currency>
- <!-- EOF JT Yahoo Finance Integration-->
Step 4 - Run it! |
In your Magento Admin go to System > Manage Currency Rates
In the drop-down, Webservicex is still the top one so simply select Yahoo Finance from there and click the Import button.
Step 5 - Optional - Cron Setup |
In Admin under System > Configuration go to Currency Setup under the Default Configuration scope. Enable the cron for Yahoo Finance here under Scheduled Import Settings (I’ve not yet tested this).
Important Notes |
Just to not piss off Yahoo, there’s a sleep(1) one second interval built in. If you only use two or three currencies, you won’t even notice this delay. If like me you use 23 currencies, this may make it look slow but at least you’ll avoid potential auto-throttling and/or Yahoo! firewall banning by making requests look more “real”.
Yahoo! supplies these rates in csv files to the public. It’s up to you whether you ass-u-me that you can now use them in an automated fashion for commercial benefit or kindly request permission, or at least read the small print.
Discussion |
If you think the implementation lacks or it can be better in a certain way, feel free to discuss here (click edit and amend this “discussion” part) and PM me to discuss. Or post in the “official” thread here: http://www.magentocommerce.com/boards/viewthread/32253/.


