https://extendsclass.com/php-bin/3c59724
both stocks and crypto to work.
I’m a novice and I would have to know more in order to understand where I’ve gone wrong on this. What I do know is tht it was working fine up to a few days ago. It seems that crypto data is being pulled fine, so it has to be something askew with the stock perameters. As far as ‘trending stocks’, that feature isn’t even being used. I believe that may be because of the tier I’m paying for.
/* CSS for the ticker marquee */ .ticker { background-color: black; color: lime; font-family: "Courier New", monospace; font-size: 16px; overflow: hidden; white-space: nowrap; } .ticker span {
display: inline-block;
padding-left: 100%;
animation: marquee 20s linear infinite;
}
@keyframes marquee {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-100%);
}
}
</style>
<?php
$apiKey = '6WETQXhh0jRuVYtCVhbtak1QX1JwtLFpZ0fRVCHq';
$stockEndpoint = 'https://api.stockdata.org/v1/data/quote';
$currencyEndpoint = 'https://api.stockdata.org/v1/data/currency/latest';
$trendingEndpoint = 'https://api.stockdata.org/v1/news/stats/trending';
function callStockdataAPI($symbol, $apiKey, $endpoint, $field, $isTwoDimensional = false)
{
$url = "{$endpoint}?api_token={$apiKey}&symbols={$symbol}&extended_hours=false&key_by_ticker=false&include_metadata=false";
try {
$response = file_get_contents($url);
$data = json_decode($response, true);
if (!isset($data['data'][0][0][$field]) && !isset($data['data'][0][$field])) return "No data available for {$symbol}";
if ($isTwoDimensional) $stockPrice = $data['data'][0][0][$field];
else $stockPrice = $data['data'][0][$field];
return "{$symbol}: $$stockPrice";
} catch (Exception $e) {
return "Error fetching stock price for {$symbol}";
}
}
$data = [];
$symbols = [
'GOOG', 'AAPL', 'TSLA', 'META', 'AMZN', 'NFLX', 'GME', 'AMC', 'NOK', 'TSM','BLK',
'BTCUSD', 'ETHUSD', 'XRPUSD'
];
foreach ($symbols as $symbol) {
if (strpos($symbol, 'USD') !== false) $data[] = callStockdataAPI($symbol, $apiKey, $currencyEndpoint, 'price', true);
else $data[] = callStockdataAPI($symbol, $apiKey, $stockEndpoint, 'price');
}
if (!empty($data)) $stockTickerText = implode(" ", $data);
else $stockTickerText = "No data available";
?>
<script>
document.getElementById('stockTicker').textContent = "<?php echo $stockTickerText; ?>";
</script>