Usage

jcsmorais edited this page Jan 26, 2013 · 13 revisions
Clone this wiki locally

Here's a list of basic usage examples, each example should be preceded by the code below:

<?php

require_once 'OpenExchangeRates/vendor/autoload.php';

$appId            = '';
$secureConnection = false;

Examples:

  1. How to retrieve available currencies list?
  2. How to retrieve a specific currency name?
  3. How to retrieve latest exchange rates?
  4. How to retrieve latest exchange rates with a base rate different than USD?
  5. How to retrieve latest exchange rates for specific Iso4217 values?
  6. How to retrieve latest exchange rates for specific Iso4217 values with a base rate different than USD?
  7. How to retrieve exchange rates for a specific date?
  8. How to retrieve exchange rates for a specific date and Iso4217 values?
  9. How to convert a specific amount from one currency to another?

How to retrieve available currencies list? 

try {
    $url = 'openexchangerates.org/api/currencies.json';

    $service    = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $currencies = new \OpenExchangeRates\Currencies\Currencies($service);

    echo "<pre>";
    print_r($currencies->fetch()->getCurrencies());

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to retrieve a specific currency name? 

try {
    $url = 'openexchangerates.org/api/currencies.json';

    $service    = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $currencies = new \OpenExchangeRates\Currencies\Currencies($service);

    var_dump($currencies->fetch()->getByIso4217('EUR'));

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to retrieve latest exchange rates? 

try {
    $url = 'openexchangerates.org/api/latest.json';

    $service = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $rates   = new \OpenExchangeRates\Rates\Rates($service);

    echo "<pre>";
    print_r($rates->fetch()->getRates());

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to retrieve latest exchange rates with a base rate different than USD? 

try {
    $url = 'openexchangerates.org/api/latest.json';

    $service = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $rates   = new \OpenExchangeRates\Rates\Rates($service);

    echo "<pre>";
    print_r($rates->fetch('EUR')->getRates());

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to retrieve latest exchange rates for specific Iso4217 values? 

try {
    $url = 'openexchangerates.org/api/latest.json';

    $service = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $rates   = new \OpenExchangeRates\Rates\Rates($service);

    echo "<pre>";
    print_r($rates->fetchByIso4217(array('EUR', 'USD', 'PHP'))->getRates());

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to retrieve latest exchange rates for specific Iso4217 values with a base rate different than USD? 

try {
    $url = 'openexchangerates.org/api/latest.json';

    $service = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $rates   = new \OpenExchangeRates\Rates\Rates($service);

    echo "<pre>";
    print_r($rates->fetchByIso4217(array('EUR', 'USD', 'PHP'), 'EUR')->getRates());

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to retrieve exchange rates for a specific date? 

try {
    $url = 'openexchangerates.org/api/historical/2013-01-01.json';

    $service = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $rates   = new \OpenExchangeRates\Rates\Rates($service);

    echo "<pre>";
    print_r($rates->fetch()->getRates());

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to retrieve exchange rates for a specific date and Iso4217 values? 

try {
    $url = 'openexchangerates.org/api/historical/2013-01-01.json';

    $service = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $rates   = new \OpenExchangeRates\Rates\Rates($service);

    echo "<pre>";
    print_r($rates->fetchByIso4217(array('EUR', 'USD', 'PHP'))->getRates());

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}

How to convert a specific amount from one currency to another? 

try {
    $url = 'openexchangerates.org/api/latest.json';

    $service = new \OpenExchangeRates\Service\Service($url, $appId, $secureConnection);
    $rates   = new \OpenExchangeRates\Rates\Rates($service);

    $amountUsd = 10;
    $amountEur = $rates->fetch()->convert($amountUsd, 'EUR');

    var_dump("'$amountUsd' USD is '$amountEur' EUR");

} catch (Exception $e) {
    echo "An error occured: " . $e->getMessage();
}