Slow page loading due to PHP API calls for streaming services

I’m developing a website that utilizes PHP to check the live status of streams from various platforms. However, I’ve encountered significant issues with long loading times, and occasionally the server times out.

My script frequently hits the 30-second execution time limit and results in the following error: Fatal error: Maximum execution time of 30 seconds exceeded.

Here’s my function for determining if a stream is live:

function checkStreamStatus($streamName, $platform){
    if($platform == 'twitch'){
        $apiEndpoint = "https://api.twitch.tv/api/streams/" . $streamName;
        $request = file_get_contents($apiEndpoint);
        $jsonResponse = json_decode($request, true);
        
        return $jsonResponse['stream'] != null;
    } else if($platform == 'youtube'){
        $streamCheckUrl = 'https://api.youtube.com/check/' . $streamName;
        $response = file_get_contents($streamCheckUrl);
        $parsedResponse = json_decode($response, true);
        
        return $parsedResponse['status'] == "live";
    }
}

I also have another function for fetching stream details:

function fetchStreamDetails($streamName, $platform){
    $streamName = sanitizeInput($streamName);
    $platform = sanitizeInput($platform);
    
    if($platform == 'twitch'){
        $streamData = json_decode(file_get_contents("https://api.twitch.tv/streams/$streamName"),true);
        
        return array(
            'thumbnail'=>$streamData['preview']['medium'],
            'stream_title'=>$streamData['channel']['status'],
            'preview'=>$streamData['preview']['large'],
            'category'=>$streamData['game']
        );
    } else if($platform == 'youtube'){
        $streamData = json_decode(file_get_contents("https://api.youtube.com/live/$streamName"),true);
        return array(
            'thumbnail'=>$streamData['snippet']['thumbnails']['medium']['url'],
            'stream_title'=>$streamData['snippet']['title'],
            'preview'=>$streamData['snippet']['thumbnails']['high']['url'],
            'category'=>$streamData['snippet']['categoryId']
        );
    }
}

Although the functions are working properly, their execution speed is concerning. I’ve noticed that other gaming websites can load similar data much quicker. Any suggestions on improving my script’s performance?

switch to guzzle http client instead of file_get_contents - it’s way better with timeouts and has async built-in. you’re hitting the same endpoints over and over, so definitely throw some redis caching in there.

totally agree, switching to curl will help a lot! also, caching can reduce those api calls since stream status doesn’t change super fast. maybe look into async calls if you want to get fancy with it!

Sequential API calls are killing your performance. I hit this same wall building a multi-platform stream aggregator last year. cURL multi-handle saved my ass - fire all requests at once instead of waiting for each one to finish. Dropped my load times from 15+ seconds to under 3 seconds checking 20+ streams. Set your connection and transfer timeouts to 5 seconds each in cURL options. Streaming APIs are flaky, so you need fallbacks or one slow response will tank everything.

Your problem is synchronous API calls - they block everything until each request finishes. I’ve built similar apps and switching to cURL with timeouts (5-10 seconds max) makes a huge difference over file_get_contents. What really helped me was adding a database cache. Store stream status with timestamps and only refresh every 2-3 minutes. Most users don’t need second-by-second updates anyway, and you’ll cut API calls dramatically. Also add error handling so one slow endpoint doesn’t kill your whole page.