Changeset 411


Ignore:
Timestamp:
Oct 1, 2012 5:41:35 PM (12 years ago)
Author:
anonymous
Message:

Added ECB XML API to Currency.inc.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Currency.inc.php

    r400 r411  
    4545        'cache_dir' => '',
    4646        'cache_age' => 86400, // 24 hours.
    47         'api_type' => 'json', // 'json' or 'soap'.
    48         'api_url' => 'http://xurrency.com/api/%s/%s/%s',
    49         'api_key' => '',
     47        'api' => 'ecb', // 'xurrency' or 'ecb'. Add other APIs under the _performAPICall() method.
     48        'api_key' => '', // Used only by xurrency API.
    5049    );
    5150   
     
    156155                // Failed retrieving value. Use cached copy for now.
    157156                $app->logMsg(sprintf('Failed getting currency exchange rate: %s-to-%s, using cached copy', $base, $target), LOG_NOTICE, __FILE__, __LINE__);
    158                 if (!$value = file_get_contents($cache_file_path)) {
     157                if (!$value = @file_get_contents($cache_file_path)) {
    159158                    $app->logMsg(sprintf('Failed reading cached exchange rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
    160159                    return false;
     
    184183    {
    185184        $app =& App::getInstance();
    186        
    187         switch ($this->getParam('api_type')) {
    188         case 'json' :
    189             $api_url = $this->getParam('api_url');
     185
     186        switch ($this->getParam('api')) {
     187        case 'xurrency' :
     188            $api_url = 'http://xurrency.com/api/%s/%s/%s';
    190189            if ('' != $this->getParam('api_key')) {
    191190                $api_url .= '?key=' . $this->getParam('api_key');
     
    212211            break;
    213212
    214         case 'soap' :
    215             // Xurrency doesn't appear to support SOAP anymore, but we'll leave this here in case it comes back.
    216 
    217             // Setup SOAP object.
    218             require_once 'SOAP/Client.php';
    219             $soap_client = new SOAP_Client($this->getParam('api_url'));
    220             // Call API
    221             $result = $soap_client->call('getValue', $parameters);
    222             // Check for errors.
    223             if (PEAR::isError($result)) {
    224                 $app->logMsg(sprintf('SOAP Pear error: %s', $result->getMessage()), LOG_WARNING, __FILE__, __LINE__);
    225                 return false;
    226             }
    227             return $result;
    228             break;
     213        case 'ecb' :
     214            // Fetch XML from ECB <http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml>
     215            $api_url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
     216            if (false === $sXML = file_get_contents($api_url)) {
     217                $app->logMsg(sprintf('Failed to load ECB XML data from ', $api_url), LOG_WARNING, __FILE__, __LINE__);
     218                return false;
     219            }
     220            if (false === $oXML = simplexml_load_string($sXML)) {
     221                $app->logMsg(sprintf('Failed to decode ECB XML data: ', truncate($sXML, 200, 'end')), LOG_WARNING, __FILE__, __LINE__);
     222                return false;
     223            }
     224            // Populate Array
     225            foreach ($oXML->Cube->Cube->Cube as $oRate) {
     226                foreach ($oRate->attributes() as $sKey => $sAttribute) {
     227                    if ($sKey == 'currency') {
     228                        $sCurrency = strtolower((string) $sAttribute);
     229                    } else if ($sKey == 'rate') {
     230                        $nRate = (string) $sAttribute;
     231                    }
     232                }
     233                $aCurrencies['eur'][$sCurrency] = $nRate;
     234                $aCurrencies[$sCurrency]['eur'] = 1 / $nRate;
     235            }
     236            // Check if requested rates are available.
     237            if (isset($aCurrencies[$parameters['base']][$parameters['target']])) {
     238                return (float) $aCurrencies[$parameters['base']][$parameters['target']];
     239            } else {
     240                $app->logMsg(sprintf('API %s does not have base %s or target %s', $this->getParam('api'), $parameters['base'], $parameters['target']), LOG_WARNING, __FILE__, __LINE__);
     241                return false;
     242            }
    229243
    230244        default :
    231             trigger_error('Unknown api_type: ' . $this->getParam('api_type'), E_USER_ERROR);
     245            trigger_error('Unknown API: ' . $this->getParam('api'), E_USER_ERROR);
    232246            die;
    233247            break;
Note: See TracChangeset for help on using the changeset viewer.