35761-vm/api/ticker.php
Flatlogic Bot 766cac14c7 V.2.2
2025-11-16 16:03:04 +00:00

116 lines
3.7 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
// --- Configuration ---
$symbols = ['bitcoin', 'ethereum']; // CoinGecko IDs
$exchange = 'CoinGecko';
$interval = '1d'; // CoinGecko provides daily data
// --- Data Fetching ---
function fetch_candlestick_data($symbol, $interval) {
$api_url = sprintf(
"https://api.coingecko.com/api/v3/coins/%s/ohlc?vs_currency=usd&days=1",
$symbol
);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $api_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => 'FlatlogicMarketDetector/1.0'
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
error_log("CoinGecko API request failed with HTTP code {$http_code}: {$response}");
return null;
}
$data = json_decode($response, true);
if (isset($data['error'])) {
error_log("CoinGecko API error: " . $data['error']);
return null;
}
return $data;
}
// --- Database Logging ---
function log_candlestick_data($pdo, $symbol, $exchange, $interval, $kline) {
$sql = <<<SQL
INSERT INTO candlestick_data (symbol, exchange, interval_time, open_time, open_price, high_price, low_price, close_price, volume, close_time, quote_asset_volume, number_of_trades, taker_buy_base_asset_volume, taker_buy_quote_asset_volume)
VALUES (:symbol, :exchange, :interval_time, :open_time, :open_price, :high_price, :low_price, :close_price, 0, 0, 0, 0, 0, 0)
ON DUPLICATE KEY UPDATE
close_price = VALUES(close_price), high_price = VALUES(high_price), low_price = VALUES(low_price), volume = VALUES(volume);
SQL;
$stmt = $pdo->prepare($sql);
$stmt->execute([
':symbol' => $symbol,
':exchange' => $exchange,
':interval_time' => $interval,
':open_time' => $kline[0],
':open_price' => $kline[1],
':high_price' => $kline[2],
':low_price' => $kline[3],
':close_price' => $kline[4],
]);
}
// --- Main Execution ---
$latest_tickers = [];
try {
$pdo = db();
foreach ($symbols as $symbol) {
$klines = fetch_candlestick_data($symbol, $interval);
if (empty($klines) || !is_array($klines)) {
continue; // Skip this symbol if data fetching fails
}
foreach ($klines as $kline) {
log_candlestick_data($pdo, $symbol, $exchange, $interval, $kline);
}
// For the frontend, provide the most recent ticker data
$latest_kline = end($klines);
$latest_tickers[] = [
'exchange' => $exchange,
'symbol' => strtoupper($symbol),
'price' => $latest_kline[4], // Close price
'change_24h_percent' => 0, // Placeholder
'signal' => '-'
];
}
} catch (Exception $e) {
http_response_code(500);
// Log error and exit gracefully
error_log('Ticker script failed: ' . $e->getMessage());
echo json_encode(['error' => 'An internal error occurred.']);
exit;
}
// --- Output for Frontend ---
if (isset($_GET['symbol'])) {
$symbol_to_find = strtoupper($_GET['symbol']);
foreach ($latest_tickers as $ticker) {
if ($ticker['symbol'] === $symbol_to_find) {
echo json_encode($ticker);
exit;
}
}
// Fallback if the requested symbol wasn't processed
echo json_encode(['error' => 'Data for symbol not found.']);
} else {
// If no symbol is specified, do not return anything for now.
// The frontend will request each symbol individually.
echo json_encode(['status' => 'OK', 'message' => 'Data processed.']);
}