PHP code example of r94ever / php-currency-exchange-rate
1. Go to this page and download the library: Download r94ever/php-currency-exchange-rate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
r94ever / php-currency-exchange-rate example snippets
use R94ever\CurrencyExchangeRate\ExchangeRate;
use R94ever\CurrencyExchangeRate\Providers\ExchangeRateHost;
use R94ever\CurrencyExchangeRate\Enums\Currency;
// Create a new ExchangeRateHost provider with your access key.
$provider = new ExchangeRateHost('YOUR_ACCESS_KEY');
// Create a new ExchangeRate instance.
$exchangeRate = new ExchangeRate($provider);
// Convert 100 USD to EUR.
$result = $exchangeRate->convert(100, Currency::USD, Currency::EUR);
echo $result; // Example output: 85.5
// Get multiple exchange rates at once
$rates = $exchangeRate->getRates(Currency::USD, [Currency::EUR, Currency::GBP, Currency::JPY]);
foreach ($rates as $rate) {
echo sprintf(
"1 %s = %s %s\n",
$rate->source->value,
$rate->rate,
$rate->target->value
);
}
/*
Output example:
1 USD = 0.85 EUR
1 USD = 0.73 GBP
1 USD = 110.25 JPY
*/
namespace App\CurrencyProviders;
use R94ever\CurrencyExchangeRate\Providers\BaseProvider;
use R94ever\CurrencyExchangeRate\Enums\Currency;
class MyCustomProvider extends BaseProvider
{
public function convert(float $amount, Currency $from, Currency $to): ?float
{
// Write your logic here to call the API and get the rate
// Example:
if ($from === Currency::USD && $to === Currency::VND) {
$rate = 25000; // Assumed rate
return $amount * $rate;
}
return null;
}
public function getRates(Currency $source, array $targets): array
{
// Write your logic here to get multiple exchange rates at once
// Example:
$rates = [];
foreach ($targets as $target) {
if ($source === Currency::USD) {
$rates[] = new CurrencyRate(
source: $source,
target: $target,
rate: match($target) {
Currency::VND => 25000,
Currency::EUR => 0.85,
Currency::GBP => 0.73,
default => 1.0
}
);
}
}
return $rates;
}
}
use R94ever\CurrencyExchangeRate\ExchangeRate;
use App\CurrencyProviders\MyCustomProvider;
$provider = new MyCustomProvider();
$exchangeRate = new ExchangeRate($provider);
$result = $exchangeRate->convert(10, Currency::USD, Currency::VND); // 250000
namespace App\HttpClients;
use R94ever\CurrencyExchangeRate\HttpDrivers\HttpClientInterface;
use R94ever\CurrencyExchangeRate\HttpDrivers\HttpResponseInterface;
use R94ever\CurrencyExchangeRate\HttpDrivers\CurlHttpResponse; // Or create your own HttpResponse
class MyCustomHttpClient implements HttpClientInterface
{
// Assuming you inject another client, e.g., Guzzle
private $guzzleClient;
public function __construct()
{
// $this->guzzleClient = new GuzzleHttp\Client();
}
public function withHeaders(array $headers): self
{
// Logic to add headers to the next request
// $this->guzzleClient->setDefaultOption('headers', $headers);
return $this;
}
public function get(string $url): HttpResponseInterface
{
// Logic to perform a GET request with your client
// $response = $this->guzzleClient->get($url);
// $body = $response->getBody()->getContents();
// $statusCode = $response->getStatusCode();
// Temporarily return a mock response
$body = '{"success": true, "rates": {"USD": 1.08}}';
$statusCode = 200;
return new CurlHttpResponse($body, $statusCode);
}
public function post(string $url, array $data): HttpResponseInterface
{
// Logic to perform a POST request
// ...
$body = '{"success": true}';
$statusCode = 200;
return new CurlHttpResponse($body, $statusCode);
}
}
namespace App\HttpClients;
use R94ever\CurrencyExchangeRate\HttpDrivers\HttpResponseInterface;
class MyCustomHttpResponse implements HttpResponseInterface
{
private $body;
private $statusCode;
public function __construct($body, $statusCode)
{
$this->body = $body;
$this->statusCode = $statusCode;
}
public function getBody(): string
{
return $this->body;
}
public function getStatusCode(): int
{
return $this->statusCode;
}
}
use R94ever\CurrencyExchangeRate\Providers\ExchangeRateHost;
use App\HttpClients\MyCustomHttpClient;
$customHttpClient = new MyCustomHttpClient();
$provider = new ExchangeRateHost('YOUR_ACCESS_KEY');
$provider->useHttpClient($customHttpClient);
// Then, use the provider as usual
$exchangeRate = new ExchangeRate($provider);
$result = $exchangeRate->convert(100, Currency::USD, Currency::EUR);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.