Example #1
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $queryPairs = sprintf('"%s%s"', $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     $query = sprintf('select * from yahoo.finance.xchange where pair in (%s)', $queryPairs);
     $url = sprintf(self::URL, urlencode($query));
     $content = $this->fetchContent($url);
     $json = StringUtil::jsonToArray($content);
     $data = $json['query']['results']['rate'];
     if ('0.00' === $data['Rate'] || 'N/A' === $data['Date']) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     $dateString = $data['Date'] . ' ' . $data['Time'];
     $date = \DateTime::createFromFormat('m/d/Y H:ia', $dateString);
     return new Rate($data['Rate'], $date, "Yahoo Finance");
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     if ('USD' !== $currencyPair->getBaseCurrency()) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     $url = sprintf(self::URL, $this->accKey);
     $content = $this->fetchContent($url);
     $data = StringUtil::jsonToArray($content);
     if (isset($data['error'])) {
         throw new Exception($data['info']);
     }
     $date = new \DateTime();
     $date->setTimestamp($data['timestamp']);
     if ($data['source'] === $currencyPair->getBaseCurrency() && isset($data['quotes']['USD' . $currencyPair->getQuoteCurrency()])) {
         return new Rate((string) $data['quotes']['USD' . $currencyPair->getQuoteCurrency()], $date, 'Currencylayer');
     }
     throw new UnsupportedCurrencyPairException($currencyPair);
 }