Official PHP client for the Commodities-API.
composer require commodities-api/sdk<?php
require 'vendor/autoload.php';
use CommoditiesApi\CommoditiesClient;
$client = new CommoditiesClient('YOUR_ACCESS_KEY');
$response = $client->getLatest([
'base' => 'USD',
'symbols' => 'RICE,WHEAT,SUGAR',
]);
print_r($response);// Latest rates
$client->getLatest([
'base' => 'USD',
'symbols' => 'RICE,WHEAT,SUGAR',
]);
// Historical (YYYY-MM-DD)
$client->getHistorical('2024-05-15', [
'base' => 'USD',
'symbols' => 'WTIOIL,GOLD',
]);
// Time-series
$client->getTimeSeries([
'start_date' => '2024-01-01',
'end_date' => '2024-01-31',
'base' => 'USD',
'symbols' => 'WTIOIL',
]);
// Fluctuation
$client->getFluctuation([
'start_date' => '2024-01-01',
'end_date' => '2024-01-31',
'base' => 'USD',
'symbols' => 'WTIOIL,GOLD',
]);
// Convert
$client->convert([
'from' => 'USD',
'to' => 'WTIOIL',
'amount' => 100,
]);
// Symbols
$client->getSymbols();
// Seasonality (cuando exista el endpoint)
$client->getSeasonality([
'symbol' => 'WTIOIL',
'group_by' => 'month',
'years' => 5,
]);use CommoditiesApi\CommoditiesApiException;
try {
$response = $client->getLatest(['base' => 'USD']);
} catch (CommoditiesApiException $e) {
echo $e->getMessage();
echo $e->getStatusCode();
var_dump($e->getRaw());
}