Website performance issues when integrating streaming platform API

I’m working on a Drupal site and trying to create a custom block that displays user streaming status similar to what you see on gaming websites. I added a custom field where users can input their streaming channel username.

Here’s my template code that fetches the data:

<?php
$start_time = microtime(true);
$username = trim(strip_tags($fields['field_channel_name']->content));
$api_response = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($username)), true);
$status_text = " is not streaming";
$viewer_count = "Offline";
$category = strip_tags($fields['field_user_team']->content);
if ($api_response['stream'] != NULL) {
    $display_name = $api_response['stream']['channel']['display_name'];
    $stream_description = $api_response['stream']['channel']['status'];
    $current_category = $api_response['stream']['channel']['game'];
    $viewer_count = $api_response['stream']['viewers']." watching";
    $status_text = " is live";
}
$end_time = microtime(true);
$load_time = $end_time - $start_time;
$milliseconds = $load_time * 1000;
?>
<div class=<?php echo "\"$category streamBox\"" ?> title=<?php echo "\"$viewer_count\"" ?>>
<?php
    print $milliseconds;
    print $fields['name']->content;
    echo "$status_text";
?>
</div>

The functionality works correctly but my site becomes extremely slow when this block loads. I’m wondering if this is caused by my implementation or if the external API calls are naturally this sluggish. Should I look into caching solutions or other optimization methods?

Cache it for sure. Had the same problem with YouTube API calls - destroyed my server. Just set up a basic DB table for the stream data and update it every 5-10 minutes with cron. Way better than hitting Twitch on every page load.

Hit the same problem building a dashboard that pulled from multiple APIs. file_get_contents blocks your entire page while waiting for the external response. If Twitch’s API is slow or times out, your users just sit there waiting. I switched to async requests with cURL multi-handle or Guzzle HTTP client. You can set proper timeouts and handle failures without breaking everything. Better yet - store the streaming status in your database and update it with a cron job every few minutes instead of fetching on every page load. Most streaming platforms rate limit anyway, so this saves you headaches down the road.

for sure, it’s the API calls that are makin it slow. file_get_contents can block until the response comes in. try using cURL with timeouts or set some caching for a few mins cause stream status doesn’t change that often.

Your bottleneck is definitely those synchronous API calls during page render. I hit this same issue building a guild roster that pulled member activity from game APIs. Your timing measurements will probably show 2-3 seconds per request - multiply that across multiple users and you’re screwed. Here’s what worked for me: cache the API responses in Drupal’s cache system with a 5-10 minute TTL for streaming status, then fall back to stale data if the API’s down. Use cache_get() and cache_set() to store responses by username. Your template renders instantly from cached data while a background process updates everything. Also, heads up - that Twitch Kraken API is deprecated. Switch to the newer Helix API, it performs way better.