Issue with Laravel project and Twitch API integration

Hey everyone, I’m working on a Laravel project that uses the Twitch API. I’m trying to show which streamers are live and hide the ones that aren’t. But I’m running into a problem.

When a streamer isn’t live, I get this error:

Trying to get property of non-object

Here’s what my code looks like:

$twitch_data = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/' . $channel_name));
$is_live = $twitch_data->stream ? 1 : 0;

$channel_info = Channels::findOrFail($channel_id);
$channel_info->preview_image = $twitch_data->stream->preview->medium;
$channel_info->current_game = $twitch_data->stream->channel->game;
$channel_info->is_streaming = $is_live;
$channel_info->save();

When a channel is offline, the API returns something like this:

{
  "stream": null,
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/channelname",
    "channel": "https://api.twitch.tv/kraken/channels/channelname"
  }
}

Can anyone help me figure out why I’m getting the non-object error? Thanks in advance!

I’ve worked extensively with the Twitch API, and I can tell you that handling offline streams is always tricky. Here’s a robust solution I’ve used in production:

$twitch_data = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/' . $channel_name));

$channel_info = Channels::findOrFail($channel_id);
$channel_info->is_streaming = !empty($twitch_data->stream);

if ($channel_info->is_streaming) {
    $channel_info->preview_image = $twitch_data->stream->preview->medium ?? null;
    $channel_info->current_game = $twitch_data->stream->channel->game ?? 'Unknown';
} else {
    // Fetch offline channel info
    $offline_data = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/' . $channel_name));
    $channel_info->preview_image = $offline_data->video_banner ?? null;
    $channel_info->current_game = null;
}

$channel_info->save();

This approach not only handles the online/offline cases but also fetches the offline channel banner. It’s more informative for users and avoids blank spots in your UI. Remember to implement proper error handling and API rate limiting to avoid potential issues down the line.

yo pete, i think i kno whats up. u gotta check if the stream exists before tryin to grab stuff from it. try this:

if ($twitch_data && isset($twitch_data->stream)) {
    $channel_info->preview_image = $twitch_data->stream->preview->medium ?? null;
    $channel_info->current_game = $twitch_data->stream->channel->game ?? 'Unknown';
    $channel_info->is_streaming = 1;
} else {
    $channel_info->is_streaming = 0;
}

this should fix ur error. good luck with ur project!

I’ve dealt with similar Twitch API issues in my projects. The problem stems from trying to access properties of null when a stream is offline. Here’s a more robust approach I’ve used:

$twitch_data = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/' . $channel_name));

$channel_info = Channels::findOrFail($channel_id);
$channel_info->is_streaming = isset($twitch_data->stream) ? 1 : 0;

if ($channel_info->is_streaming) {
    $channel_info->preview_image = $twitch_data->stream->preview->medium ?? null;
    $channel_info->current_game = $twitch_data->stream->channel->game ?? 'Unknown';
} else {
    $channel_info->preview_image = null;
    $channel_info->current_game = null;
}

$channel_info->save();

This handles both online and offline cases gracefully. It also uses the null coalescing operator (??) to provide default values, further improving robustness. Hope this helps solve your issue!

I’ve encountered this issue before when working with the Twitch API. The problem occurs because you’re trying to access properties of a null object when a stream is offline. Here’s a more reliable approach:

$twitch_data = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/' . $channel_name));

$channel_info = Channels::findOrFail($channel_id);
$channel_info->is_streaming = !empty($twitch_data->stream);

if ($channel_info->is_streaming) {
    $channel_info->preview_image = $twitch_data->stream->preview->medium ?? null;
    $channel_info->current_game = $twitch_data->stream->channel->game ?? 'N/A';
} else {
    $channel_info->preview_image = null;
    $channel_info->current_game = null;
}

$channel_info->save();

This code checks if the stream exists before attempting to access its properties, preventing the ‘non-object’ error. It also uses null coalescing to handle potential missing data. Make sure to implement proper error handling for API requests as well.

hey pete, i ran into something similar before. looks like ur assuming the stream data always exists. try adding some checks before accessing those properties:

$is_live = isset($twitch_data->stream) ? 1 : 0;
if ($is_live) {
    $channel_info->preview_image = $twitch_data->stream->preview->medium;
    $channel_info->current_game = $twitch_data->stream->channel->game;
}

that should prevent the error when streams r offline. lmk if it helps!