1. Go to this page and download the library: Download silencenjoyer/api-sdk 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/ */
class NbuApi extends AbstractApi
{
private string $baseUrl;
public function __construct(
string $baseUrl,
RequestBuilderInterface $requestBuilder,
ResponseParserInterface $responseParser,
HandlerInterface $handler,
MiddlewareStack $middlewareStack
) {
parent::__construct($requestBuilder, $responseParser, $handler, $middlewareStack);
$this->baseUrl = $baseUrl;
}
protected function getUrl(): string
{
return $this->baseUrl;
}
public static function forUrl(string $baseUrl): ApiBuilderInterface
{
return static::getBuilder()
->withFactory(fn($rb, $rp, $h, $ms) => new self($baseUrl, $rb, $rp, $h, $ms));
}
}
// Usage:
$api = NbuApi::forUrl($_ENV['NBU_API_URL'])->build();
$api = NbuApi::forUrl($_ENV['NBU_API_URL'])->withMiddlewares([$logger])->build();
use Silencenjoyer\ApiSdk\Commands\AbstractCommand;
use Silencenjoyer\ApiSdk\Constants\HttpMethod;
class GetExchangeRatesCommand extends AbstractCommand implements NbuCommandInterface
{
public function __construct(private string $date) {}
protected function isPublic(): bool
{
return true;
}
protected function getMethod(): string
{
return HttpMethod::GET;
}
protected function getPath(): string
{
return '/NBUStatService/v1/statdirectory/exchange';
}
protected function getQueryParams(): array
{
return ['json' => '', 'date' => $this->date];
}
}
$api = NbuApi::getBuilder()
->withCommandMiddleware(GetExchangeRatesCommand::class, new RateLimiterMiddleware())
->build();
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Silencenjoyer\ApiSdk\Handlers\HandlerInterface;
use Silencenjoyer\ApiSdk\Middlewares\MiddlewareInterface;
class RetryMiddleware implements MiddlewareInterface
{
public function handle(RequestInterface $request, HandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if ($response->getStatusCode() === 503) {
$response = $handler->handle($request);
}
return $response;
}
}
use Silencenjoyer\ApiSdk\Constants\Format;
NbuApi::getBuilder()
->withAddedParser(Format::JSON, new MyJsonParser())
->withAddedSerializer('xml', new XmlSerializer())
->withFormats(Format::JSON, 'xml')
->build();
// Option 1 — override on the command
protected function getRequestContentType(): ?ContentTypeDto
{
return new ContentTypeDto(Format::JSON);
}
// Option 2 — set the header, the SDK reads it
protected function getHeaders(): array
{
return ['Content-Type' => 'application/json'];
}
// Force a specific parser regardless of the response header
protected function getAnswerContentType(): ?ContentTypeDto
{
return new ContentTypeDto(Format::JSON);
}