00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Directory_Model_Currency_Import_Webservicex extends Mage_Directory_Model_Currency_Import_Abstract
00035 {
00036 protected $_url = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency={{CURRENCY_FROM}}&ToCurrency={{CURRENCY_TO}}';
00037 protected $_messages = array();
00038
00039
00040
00041
00042
00043
00044 protected $_httpClient;
00045
00046 public function __construct()
00047 {
00048 $this->_httpClient = new Varien_Http_Client();
00049 }
00050
00051 protected function _convert($currencyFrom, $currencyTo, $retry=0)
00052 {
00053 $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url);
00054 $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);
00055
00056 try {
00057 $response = $this->_httpClient
00058 ->setUri($url)
00059 ->setConfig(array('timeout' => Mage::getStoreConfig('currency/webservicex/timeout')))
00060 ->request('GET')
00061 ->getBody();
00062
00063 $xml = simplexml_load_string($response, null, LIBXML_NOERROR);
00064 if( !$xml ) {
00065 $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
00066 return null;
00067 }
00068 return (float) $xml;
00069 }
00070 catch (Exception $e) {
00071 if( $retry == 0 ) {
00072 $this->_convert($currencyFrom, $currencyTo, 1);
00073 } else {
00074 $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
00075 }
00076 }
00077 }
00078 }