1: <?php
2: /**
3: * Magento
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: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Directory
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Currency rate import model (From www.webservicex.net)
29: *
30: * @category Mage
31: * @package Mage_Directory
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Directory_Model_Currency_Import_Webservicex extends Mage_Directory_Model_Currency_Import_Abstract
35: {
36: protected $_url = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency={{CURRENCY_FROM}}&ToCurrency={{CURRENCY_TO}}';
37: protected $_messages = array();
38:
39: /**
40: * HTTP client
41: *
42: * @var Varien_Http_Client
43: */
44: protected $_httpClient;
45:
46: public function __construct()
47: {
48: $this->_httpClient = new Varien_Http_Client();
49: }
50:
51: protected function _convert($currencyFrom, $currencyTo, $retry=0)
52: {
53: $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url);
54: $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);
55:
56: try {
57: $response = $this->_httpClient
58: ->setUri($url)
59: ->setConfig(array('timeout' => Mage::getStoreConfig('currency/webservicex/timeout')))
60: ->request('GET')
61: ->getBody();
62:
63: $xml = simplexml_load_string($response, null, LIBXML_NOERROR);
64: if( !$xml ) {
65: $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s.', $url);
66: return null;
67: }
68: return (float) $xml;
69: }
70: catch (Exception $e) {
71: if( $retry == 0 ) {
72: $this->_convert($currencyFrom, $currencyTo, 1);
73: } else {
74: $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s.', $url);
75: }
76: }
77: }
78: }
79: