-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
58 lines (44 loc) · 1.66 KB
/
Copy pathapp.php
File metadata and controls
58 lines (44 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
/**
* Runnable Slim 4 example for the UniRate integration.
*
* composer require unirate/slim slim/slim slim/psr7 nyholm/psr7 guzzlehttp/guzzle
* UNIRATE_API_KEY=your-key php -S localhost:8080 examples/app.php
*
* Then try:
* curl "http://localhost:8080/unirate/rate?from=USD&to=EUR"
* curl "http://localhost:8080/unirate/convert?from=USD&to=EUR&amount=100"
* curl "http://localhost:8080/unirate/currencies"
* curl "http://localhost:8080/unirate/vat?country=DE"
*/
require __DIR__ . '/../vendor/autoload.php';
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;
$apiKey = getenv('UNIRATE_API_KEY') ?: '';
// Any PSR-18 client + PSR-17 factories work. Guzzle is used here as an example.
$guzzle = new GuzzleClient();
$factory = new HttpFactory();
$client = new UniRateClient(
new UniRateConfig($apiKey),
$guzzle, // PSR-18 ClientInterface
$factory, // PSR-17 RequestFactoryInterface
$factory, // PSR-17 UriFactoryInterface
);
$app = AppFactory::create();
// Attach the client to every request so route handlers can resolve it.
$app->add(new UniRateMiddleware($client));
// Register GET /unirate/{rate,convert,currencies,vat}.
registerUniRateRoutes($app);
// You can also use the client directly in your own routes:
$app->get('/', function ($request, $response) use ($client) {
$rate = $client->getRate('USD', 'EUR');
$response->getBody()->write("1 USD = {$rate} EUR");
return $response;
});
$app->run();