From c94ab13f1f91d212d9130156650b46d8ea7fc921 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Thu, 19 Oct 2023 09:44:59 -0400 Subject: [PATCH] price api --- price.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 price.php diff --git a/price.php b/price.php new file mode 100644 index 0000000..c0c60f4 --- /dev/null +++ b/price.php @@ -0,0 +1,113 @@ + [ + 'header' => $coinmarketcap_headers + ] +]); + +$coinmarketcap_response = @file_get_contents($coinmarketcap_url, false, $coinmarketcap_context); // Use '@' to suppress warnings +if ($coinmarketcap_response !== false) { + $coinmarketcap_data = json_decode($coinmarketcap_response, true); +} + +// Fetch data from blockchain.info +$blockchain_info_response = @file_get_contents($blockchain_info_url); // Use '@' to suppress warnings +if ($blockchain_info_response !== false) { + $blockchain_info_data = json_decode($blockchain_info_response, true); +} + +// Fetch data from Coinbase +$coinbase_response = @file_get_contents($coinbase_url); // Use '@' to suppress warnings +if ($coinbase_response !== false) { + $coinbase_data = json_decode($coinbase_response, true); +} + +// Extract the "15m" price from blockchain.info +if (isset($blockchain_info_data['USD']['15m'])) { + $blockchain_15m_price = $blockchain_info_data['USD']['15m']; +} + +// Extract the Coinbase amount +if (isset($coinbase_data['data']['amount'])) { + $coinbase_price = (float)$coinbase_data['data']['amount']; +} + +// Check if data was successfully retrieved from CoinGecko +if (isset($coingecko_data['bitcoin']['usd'])) { + $coingecko_price = $coingecko_data['bitcoin']['usd']; +} + +// Check if data was successfully retrieved from CoinMarketCap +if (isset($coinmarketcap_data['data']['1']['quote']['USD']['price'])) { + $coinmarketcap_price = $coinmarketcap_data['data']['1']['quote']['USD']['price']; +} + +// Check if data was retrieved from at least one source +if ($coingecko_price !== null || $coinmarketcap_price !== null || $blockchain_15m_price !== null || $coinbase_price !== null) { + // Calculate the average price using available data + $valid_data_count = 0; + $sum_prices = 0; + + if ($coingecko_price !== null) { + $sum_prices += $coingecko_price; + $valid_data_count++; + } + + if ($coinmarketcap_price !== null) { + $sum_prices += $coinmarketcap_price; + $valid_data_count++; + } + + if ($blockchain_15m_price !== null) { + $sum_prices += $blockchain_15m_price; + $valid_data_count++; + } + + if ($coinbase_price !== null) { + $sum_prices += $coinbase_price; + $valid_data_count++; + } + + $average_price = $valid_data_count > 0 ? $sum_prices / $valid_data_count : null; + + // Format the average price to display only two decimal places + if ($average_price !== null) { + $average_price = number_format($average_price, 2); + } + + // Create the response array + $responseArray = [ + 'Price' => $average_price + ]; + + // Return the response as JSON + echo json_encode($responseArray); +} else { + // Handle the case where data retrieval failed from all sources + echo json_encode(['error' => 'Unable to fetch Bitcoin price from all sources.']); +} +?>