Исторические данные для товаров
Чтобы получить все исторические цены для выбранного товара, нужно использовать входную точку "commodity-history":
https://tr.porti.ru/xapi/commodity/commodity-history?code=imoex&token=〈ТОКЕН〉&format=json
curl --location "https://tr.porti.ru/xapi/commodity/commodity-history?code=imoex&token=〈ТОКЕН〉&format=json"
import requests
url = f'https://tr.porti.ru/xapi/commodity/commodity-history?code=imoex&token=〈ТОКЕН〉&format=json'
data = requests.get(url).json()
print(data)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://tr.porti.ru/xapi/commodity/commodity-history?code=imoex&token=〈ТОКЕН〉&format=json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$data = curl_exec($curl);
curl_close($curl);
try {
$data = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
var_dump($data);
} catch (Exception $e) {
echo 'Error. '.$e->getMessage();
}
library(httr)
library(jsonlite)
url <- 'https://tr.porti.ru/xapi/commodity/commodity-history?code=imoex&token=〈ТОКЕН〉&format=json'
response <- GET(url)
if (http_type(response) == "application/json") {
content <- content(response, "text", encoding = "UTF-8")
cat(content)
} else {
cat("Error while receiving data\n")
}
Пример полученных данных с использованием этого API:
[
{
code: "brent-crude-oil",
time: "1719781200",
price: "85.579"
},
{
code: "brent-crude-oil",
time: "1719522000",
price: "84.816"
},
{
code: "brent-crude-oil",
time: "1719435600",
price: "86.503"
}
]
Параметры:
- token (обязательный) - токен получаемый при оформлении подписки (подписка API)
- code (обязательный) - тикер товара, все доступные товары можно посмотреть в списке товаров, например brent-crude-oil
- format (не обязательный) - допустимые значения: json, csv. json - представление данных в формате json, csv - текстовый формат с разделителем.