-
Notifications
You must be signed in to change notification settings - Fork 5
Usage
jcsmorais edited this page Jan 26, 2013
·
13 revisions
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;
- How to retrieve available currencies list?
- How to retrieve a specific currency name?
- How to retrieve latest exchange rates?
- How to retrieve latest exchange rates with a base rate different than USD?
- How to retrieve latest exchange rates for specific Iso4217 values?
- How to retrieve latest exchange rates for specific Iso4217 values with a base rate different than USD?
- How to retrieve exchange rates for a specific date?
- How to retrieve exchange rates for a specific date and Iso4217 values?
- How to convert a specific amount from one currency to another?
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}