php for a website not pulling stock data. endpoints and api key correct

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>

I need to stress that this solution is insecure and should only be used for local testing, not for production use. For a production environment, you should try to properly configure your server for SSL verification.

<body>
    <div class="ticker">
        <span id="stockTicker"></span>
    </div>

    <?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';

    stream_context_set_default([
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]);

    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>

</body>

I have usedstream_context_set_default at the beginning of your script to disable SSL verification. But remember, this is not recommended for a production environment as it is insecure.

please make sure you get a new API key for security reasons.
good luck

Sponsor our Newsletter | Privacy Policy | Terms of Service