35761-vm/api/ticker.php
Flatlogic Bot aa472d16a2 V.1.12
2025-11-15 09:32:44 +00:00

69 lines
2.0 KiB
PHP

<?php
header('Content-Type: application/json');
// --- Configuration ---
$symbol = 'BTCUSDT'; // Bitget symbol for BTC/USDT spot market
$api_url = "https://api.bitget.com/api/v2/spot/market/tickers?symbol=" . $symbol;
// --- cURL Execution ---
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $api_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Accept: application/json'
],
// It's good practice to set a user-agent
CURLOPT_USERAGENT => 'FlatlogicMarketDetector/1.0'
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
// --- Response Handling ---
if ($error) {
http_response_code(500);
echo json_encode(['error' => 'cURL Error: ' . $error]);
exit;
}
if ($http_code !== 200) {
http_response_code($http_code);
echo json_encode(['error' => 'Bitget API returned non-200 status', 'details' => json_decode($response)]);
exit;
}
$data = json_decode($response, true);
// --- Data Validation & Output ---
if (json_last_error() !== JSON_ERROR_NONE || empty($data['data'])) {
http_response_code(500);
echo json_encode(['error' => 'Failed to parse JSON response or data is empty']);
exit;
}
// Extract the first ticker object from the response data array
$ticker_data = $data['data'][0] ?? null;
if (!$ticker_data) {
http_response_code(404);
echo json_encode(['error' => 'Ticker data for symbol not found in API response']);
exit;
}
// --- Sanitize and Structure Output ---
// We select and sanitize the fields we want to send to the frontend.
$output = [
'exchange' => 'Bitget',
'symbol' => $ticker_data['symbol'] ?? 'N/A',
'price' => $ticker_data['lastPr'] ?? '0.00',
// Bitget provides the 24h change as a decimal, e.g., 0.025 for +2.5%
'change_24h_percent' => isset($ticker_data['priceChangePercent']) ? (float)$ticker_data['priceChangePercent'] * 100 : 0,
'signal' => '-' // Placeholder for future signal detection
];
echo json_encode($output);