Return to Snippet

Revision: 58509
at July 19, 2012 07:17 by bogdandev


Initial Code
<?php
/**
 * Currency rate import model (From www.webservicex.net)
 *
 * @category   Mage
 * @package    Mage_Directory
 * @author      Magento Core Team <[email protected]>
 */
class Mage_Directory_Model_Currency_Import_Webservicex extends Mage_Directory_Model_Currency_Import_Abstract
{
//  protected $_url = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency={{CURRENCY_FROM}}&ToCurrency={{CURRENCY_TO}}';
    protected $_url = 'http://www.google.com/ig/calculator?hl=en&q=1{{CURRENCY_TO}}=?{{CURRENCY_FROM}}';
    protected $_messages = array();

     /**
     * HTTP client
     * @var Varien_Http_Client
     */
    protected $_httpClient;

    public function __construct()
    {
        $this->_httpClient = new Varien_Http_Client();
    }

    protected function _convert($currencyFrom, $currencyTo, $retry=0)
    {
        $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url);
        $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);

        try {
            $response = $this->_httpClient
                ->setUri($url)
                ->setConfig(array('timeout' => Mage::getStoreConfig('currency/webservicex/timeout')))
                ->request('GET')
                ->getBody();

			$data = explode('"', $response);
			$data = explode(' ', $data[3]);
			$return = round($data[0], 5);
			
			return $return;
        }
        catch (Exception $e) {
            if( $retry == 0 ) {
                $this->_convert($currencyFrom, $currencyTo, 1);
            } else {
                $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s.', $url);
            }
        }
    }
}

Initial URL


Initial Description
replace /app/code/core/Mage/Directory/Model/Currency/Import/Webservicex.php

Initial Title
magento webservicex with google convertor URL

Initial Tags
magento

Initial Language
PHP