Using Yahoo! Finance for Currency Exchange Rate Retrieval

Last modified by J.T. on Fri, June 25, 2010 09:55
Source|Old Revisions  

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

  1. <?php
  2. /**
  3. * JT
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * @category   JT
  16. * @package    JT_Directory
  17. * @copyright  Copyright (c) 2009 JT
  18. * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  19. *
  20. * All credits to Magento Core Development Team who's Webservicex code I liberally borrowed
  21. * as well as Mark Alexander Bain's idea and code as displayed on the following url, accessed Feb 16th 2009
  22. * http://ajax-programming.suite101.com/article.cfm/currency_conversion_with_yahoo_finance
  23. */
  24.  
  25. /**
  26. * Currency rate import model (From quote.yahoo.com)
  27. *
  28. * @category   JT
  29. * @package    JT_Directory
  30. * @author      Magento Enthusiast <J.T.>
  31. */
  32. class JT_Directory_Model_Currency_Import_Yahoofinance extends Mage_Directory_Model_Currency_Import_Abstract
  33. {
  34.     protected $_url = 'http://quote.yahoo.com/d/quotes.csv?s={{CURRENCY_FROM}}{{CURRENCY_TO}}=X&f=l1&e=.csv';
  35.     protected $_messages = array();
  36.  
  37.     protected function _convert($currencyFrom, $currencyTo, $retry=0)
  38.     {
  39.         $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url);
  40.         $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);
  41.  
  42.         try {
  43.             sleep(1); //Be nice to Yahoo, they don't have a lot of hi-spec servers
  44.  
  45.             $handle = fopen($url, "r");
  46.  
  47.             $exchange_rate = fread($handle, 2000);
  48.  
  49.             fclose($handle);
  50.  
  51.             if( !$exchange_rate ) {
  52.                 $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
  53.                 return null;
  54.             }
  55.             return (float) $exchange_rate * 1.0; // change 1.0 to influence rate;
  56.         }
  57.         catch (Exception $e) {
  58.             if( $retry == 0 ) {
  59.                 $this->_convert($currencyFrom, $currencyTo, 1);
  60.             } else {
  61.                 $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
  62.             }
  63.         }
  64.     }
  65. }

Step 3 - Amend local.xml

That’s /app/etc/local.xml - add this after the <global> tag:

  1.         <!-- JT Yahoo Finance Integration-->
  2.         <currency>
  3.             <import>
  4.                 <services>
  5.                     <yahoofinance>
  6.                         <name>Yahoo Finance</name>
  7.                         <model>JT_Directory_Model_Currency_Import_Yahoofinance</model>
  8.                     </yahoofinance>
  9.                 </services>
  10.             </import>
  11.         </currency>
  12.         <!-- 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/.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs