Issues with extracting data from Twitch API response in PHP

I’m trying to fetch stream data from the Twitch API but running into some problems with my PHP code. Here’s what I’m working with:

<?php
    $api_response = file_get_contents("https://api.twitch.tv/kraken/streams");
    $decoded_data = json_decode($api_response, true);
    foreach ($decoded_data as $stream_info) {
        $stream_details = get_object_vars($stream_info);
        print_r(array_keys($stream_details));
    }
?>

I keep getting these error messages:

get_object_vars() expects parameter 1 to be object, array given
array_keys() expects parameter 1 to be array, null given

Can someone help me understand what’s going wrong here? I thought I was handling the JSON correctly but clearly something is off with how I’m processing the data structure.

make sure your API endpoint is correct - that Kraken URL might be returning an error or empty data since it’s deprecated. that’s more likely why you’re getting null from array_keys(). also, just remove the get_object_vars line since you’re already dealing with arrays from json_decode when you passed true.

I ran into this same issue with APIs that return nested data. The problem is how you’re handling the JSON response structure. When you decode with true, you get arrays everywhere, but Twitch wraps the actual stream data inside a container object. You’re looping through the top-level response (which has metadata and the streams array) instead of the individual streams. Drop the get_object_vars() call - you’re already working with arrays. Just access the nested streams data directly before your loop. I’d add some debugging output first to see the actual response structure. That’ll show you exactly what keys exist at each level.

You’re mixing up arrays and objects. When you use json_decode($api_response, true), everything becomes an associative array. But then you’re calling get_object_vars() which only works on objects - that’s why it’s failing. I made the same mistake when I started with Twitch’s API. The response has a ‘streams’ key that contains the actual data, so you’re probably looping through the wrong thing. Try $decoded_data['streams'] instead of $decoded_data. And since you already have arrays (thanks to that true parameter), just use array_keys($stream_info) directly - no need for get_object_vars(). Also heads up - that kraken API endpoint was deprecated ages ago. You should switch to the Helix API, but you’ll need proper auth headers for that.

The problem is you’re misunderstanding how Twitch’s API response works. I ran into this exact issue before - the API sends back a nested object where the actual stream data lives inside a ‘streams’ array. Your foreach loop is trying to iterate over the main response object instead of the streams. Ditch the get_object_vars() call completely. Since you’re using true in json_decode(), you’re already working with arrays. Just access $decoded_data['streams'] for your loop. Also heads up - that kraken endpoint is dead. You’ll need to switch to the Helix API and set up OAuth authentication. For now though, throw a var_dump($decoded_data) in there to see what the response actually looks like.

yep, classic mistake! you’re passing true to json_decode() which gives you arrays, but your code expects objects. just swap get_object_vars($stream_info) with array_keys($stream_info) and ur all set!