You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.2 KiB
79 lines
2.2 KiB
<?php |
|
header('Content-Type: application/json'); |
|
header('Access-Control-Allow-Origin: *'); |
|
header('Access-Control-Allow-Methods: GET, POST'); |
|
header('Access-Control-Allow-Headers: Content-Type'); |
|
|
|
$tickerFile = 'ticker-data.json'; |
|
$maxItems = 25; // Keep last 25 ticker items |
|
|
|
// Read existing ticker data |
|
function readTickerData($file) { |
|
if (!file_exists($file)) { |
|
return []; |
|
} |
|
$content = file_get_contents($file); |
|
return json_decode($content, true) ?: []; |
|
} |
|
|
|
// Write ticker data |
|
function writeTickerData($file, $data) { |
|
return file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT)); |
|
} |
|
|
|
// Handle different HTTP methods |
|
$method = $_SERVER['REQUEST_METHOD']; |
|
|
|
if ($method === 'GET') { |
|
// Return current ticker data |
|
$tickerData = readTickerData($tickerFile); |
|
echo json_encode($tickerData); |
|
|
|
} elseif ($method === 'POST') { |
|
// Add new ticker item |
|
$input = json_decode(file_get_contents('php://input'), true); |
|
|
|
if (!$input || !isset($input['text']) || !isset($input['type'])) { |
|
http_response_code(400); |
|
echo json_encode(['error' => 'Invalid input. Required: text, type']); |
|
exit; |
|
} |
|
|
|
$tickerData = readTickerData($tickerFile); |
|
|
|
// Add new item with timestamp |
|
$newItem = [ |
|
'text' => $input['text'], |
|
'type' => $input['type'], // 'buy' or 'sell' |
|
'timestamp' => time() |
|
]; |
|
|
|
// Add optional SNS fields if provided |
|
if (isset($input['fullAddress'])) { |
|
$newItem['fullAddress'] = $input['fullAddress']; |
|
} |
|
if (isset($input['snsName'])) { |
|
$newItem['snsName'] = $input['snsName']; |
|
} |
|
|
|
// Add to beginning of array (most recent first) |
|
array_unshift($tickerData, $newItem); |
|
|
|
// Keep only the last N items |
|
if (count($tickerData) > $maxItems) { |
|
$tickerData = array_slice($tickerData, 0, $maxItems); |
|
} |
|
|
|
// Save to file |
|
if (writeTickerData($tickerFile, $tickerData)) { |
|
echo json_encode(['success' => true, 'itemsCount' => count($tickerData)]); |
|
} else { |
|
http_response_code(500); |
|
echo json_encode(['error' => 'Failed to save ticker data']); |
|
} |
|
|
|
} else { |
|
http_response_code(405); |
|
echo json_encode(['error' => 'Method not allowed']); |
|
} |
|
?>
|