YooBalance/index.php

61 lines
2.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// Устанавливаем токен доступа
$token = 'вставьте сюда полученный токен';
// URL для запроса баланса
$accountInfoUrl = 'https://yoomoney.ru/api/account-info';
// URL для запроса списка операций
$operationHistoryUrl = 'https://yoomoney.ru/api/operation-history';
// Функция для выполнения запроса к API
function apiRequest($url, $token, $params = array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
if (!empty($params)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
die('Ошибка cURL: ' . $error);
}
curl_close($ch);
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die('Ошибка декодирования JSON: ' . json_last_error_msg());
}
if (isset($data['error'])) {
die('Ошибка API: ' . $data['error']);
}
return $data;
}
// Получаем информацию о счете
$accountInfo = apiRequest($accountInfoUrl, $token);
// Выводим баланс
$balance = $accountInfo['balance'];
echo '<h2><b>Баланс вашего кошелька: ' . $balance . ' руб.</h2></b>';
// Получаем историю операций (последние 5 операций)
$params = array(
'records' => 5,
);
$operationHistory = apiRequest($operationHistoryUrl, $token, $params);
// Выводим последние 5 операций
echo 'Последние 5 операций:<br>';
foreach ($operationHistory['operations'] as $operation) {
echo 'Дата: ' . $operation['datetime'] . '<br>';
echo 'Сумма: ' . $operation['amount'] . ' руб.<br>';
echo 'Тип: ' . $operation['title'] . '<br>';
echo 'Описание: ' . $operation['message'] . '<br>';
echo '<br>';
}
?>