Example #1
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);
 }
Example #2
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 #3
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $url = sprintf(self::URL, $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     $content = $this->fetchContent($url);
     $document = new \DOMDocument();
     @$document->loadHTML($content);
     $xpath = new \DOMXPath($document);
     $nodes = $xpath->query('//span[@class="bld"]');
     if (0 === $nodes->length) {
         throw new Exception('The currency is not supported or Google changed the response format');
     }
     $nodeContent = $nodes->item(0)->textContent;
     $bid = strstr($nodeContent, ' ', true);
     if (!is_numeric($bid)) {
         throw new Exception('The currency is not supported or Google changed the response format');
     }
     return new Rate($bid, new \DateTime(), "Google Finance");
 }