Twitch streaming status check causing website performance issues

I have implemented a feature that uses Twitch’s API to determine whether a streamer is currently broadcasting or not. While the functionality works correctly, it’s creating major performance problems for my website. The page load time has increased dramatically to anywhere between 5 to 10 seconds whenever this check runs.

I’m wondering what approaches I could take to improve this situation. Would implementing caching mechanisms like cookies or session storage help? Are there other optimization strategies I should consider?

Here’s my current implementation:

public function checkStreamerStatus($channelName){
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => 'https://api.twitch.tv/kraken/streams/'.$channelName
    ));

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    $searchTerm = "language";
    $foundPos = strpos($result, $searchTerm);

    curl_close($ch);

    if ($foundPos === false) {
        // Stream offline
    } else {
        if($channelName != null){
            echo "streamActive";
        }
    }
}

Your code has a few major issues causing the performance problems. First, you’re using the old Kraken API endpoint that got shut down in 2022 - that’s why you’re getting slow responses or failures. You need to switch to the new Helix API. The bigger problem is you’re making this API call synchronously during page load. Don’t do that. External HTTP requests should never block your page rendering. Use JavaScript to fetch the stream status after the page loads, or set up a background job to check stream statuses every few minutes and store results in your database. For caching, definitely use Redis or memcached with a 2-3 minute TTL since stream status doesn’t need to be real-time. Also make sure you include proper authorization headers with a valid client ID and bearer token when you switch to Helix API.