Page MenuHomePhorge

OpenExchangeRates.php
No OneTemporary

OpenExchangeRates.php

<?php
namespace App\Backends;
class OpenExchangeRates
{
/**
* Import exchange rates from openexchangerates.org
*
* @param string $baseCurrency Base currency
*
* @return array exchange rates
*/
public static function retrieveRates($baseCurrency)
{
$baseCurrency = strtoupper($baseCurrency);
$apiKey = \config('services.openexchangerates.api_key');
$query = http_build_query(['app_id' => $apiKey, 'base' => 'USD']);
$url = 'https://openexchangerates.org/api/latest.json?' . $query;
$html = file_get_contents($url, false);
$rates = [];
if ($html && ($result = json_decode($html, true)) && !empty($result['rates'])) {
foreach ($result['rates'] as $code => $rate) {
$rates[strtoupper($code)] = $rate;
}
if ($baseCurrency != 'USD') {
if ($base = $rates[$baseCurrency]) {
foreach ($rates as $code => $rate) {
$rates[$code] = $rate / $base;
}
} else {
$rates = [];
}
}
foreach ($rates as $code => $rate) {
\Log::debug(sprintf("Update %s: %0.8f", $code, $rate));
}
} else {
throw new \Exception("Failed to parse exchange rates");
}
if (count($rates) > 1) {
$rates[$baseCurrency] = 1;
return $rates;
}
throw new \Exception("Failed to retrieve exchange rates");
}
/**
* Validates that openexchange is available as configured.
*
* @throws \Exception
*/
public static function healthcheck(): void
{
$apiKey = \config('services.openexchangerates.api_key');
if (!empty($apiKey)) {
$query = http_build_query(['app_id' => $apiKey]);
$url = 'https://openexchangerates.org/api/usage.json' . $query;
$html = file_get_contents($url, false);
if ($html && ($result = json_decode($html, true)) && !empty($result['status'])) {
print($result);
}
throw new \Exception("Failed to retrieve exchange rates status");
}
}
}

File Metadata

Mime Type
text/x-php
Expires
Fri, Apr 4, 8:42 PM (2 h, 57 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
175706
Default Alt Text
OpenExchangeRates.php (2 KB)

Event Timeline