You are here

public function EuropeanCentralBankExchanger::getRemoteData in Commerce Exchanger 8

Fetch external data.

Parameters

string|null $base_currency: If we fetch data based on specific currency.

Overrides ExchangerProviderRemoteInterface::getRemoteData

File

src/Plugin/Commerce/ExchangerProvider/EuropeanCentralBankExchanger.php, line 31

Class

EuropeanCentralBankExchanger
Provides EuropeanCentralBank.

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider

Code

public function getRemoteData($base_currency = NULL) {
  $data = NULL;
  $request = $this
    ->apiClient([]);
  if ($request) {
    try {
      $xml = new SimpleXMLElement($request);
    } catch (\Exception $e) {
      $this->logger
        ->error($e
        ->getMessage());
    }
    $data = [];

    // Loop and build array.
    foreach ($xml->Cube->Cube->Cube as $rate) {
      $code = (string) $rate['currency'];
      $rate = (string) $rate['rate'];
      $data[$code] = $rate;
    }
  }
  return $data;
}