PHP code example of unirate / slim

1. Go to this page and download the library: Download unirate/slim 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/ */

    

unirate / slim example snippets


use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory;
use Slim\Factory\AppFactory;
use UniRate\Slim\UniRateClient;
use UniRate\Slim\UniRateConfig;
use UniRate\Slim\UniRateMiddleware;
use function UniRate\Slim\registerUniRateRoutes;

$guzzle  = new GuzzleClient();
$factory = new HttpFactory();

$client = new UniRateClient(
    new UniRateConfig(getenv('UNIRATE_API_KEY')),
    $guzzle,   // PSR-18 ClientInterface
    $factory,  // PSR-17 RequestFactoryInterface
    $factory,  // PSR-17 UriFactoryInterface
);

$app = AppFactory::create();
$app->add(new UniRateMiddleware($client));
registerUniRateRoutes($app);          // adds /unirate/rate, /convert, /currencies, /vat
$app->run();

$config = new UniRateConfig('your-key');                       // default base URL
$config = UniRateConfig::fromArray([
    'api_key'  => 'your-key',
    'base_url' => 'https://api.unirateapi.com',
]);

registerUniRateRoutes($app, '/unirate', $client);

$rate       = $client->getRate('USD', 'EUR');       // 0.92
$allRates   = $client->getRate('USD');              // ['EUR' => 0.92, ...]
$converted  = $client->convert('EUR', 100, 'USD');  // 92.5
$currencies = $client->getSupportedCurrencies();    // ['USD', 'EUR', ...]
$vat        = $client->getVatRates('DE');           // ['country_name' => 'Germany', 'vat_rate' => 19.0]

use UniRate\Slim\UniRateClient;
use UniRate\Slim\UniRateMiddleware;

$app->get('/price', function ($request, $response) {
    /** @var UniRateClient $unirate */
    $unirate = $request->getAttribute(UniRateMiddleware::ATTRIBUTE);
    $eur = $unirate->convert('EUR', 100, 'USD');
    $response->getBody()->write("€{$eur}");
    return $response;
});