I have a working PHP script that fetches stream data from Twitch API. Here’s what I’m currently using:
$api_response = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($username)), true);
if ($api_response['stream'] != null) {
$gameTitle = $api_response['stream']['channel']['game'];
$displayName = $api_response['stream']['channel']['display_name'];
$streamStatus = $api_response['stream']['channel']['status'];
$viewerCount = $api_response['stream']['viewers'];
}
This works fine for individual streams. However, I’m trying to access the “self” field from API responses that include game filters and limits. When I use something like $selfUrl = $api_response['streams']['self'], it returns empty. How can I properly extract the self URL from filtered API responses?
Your issue comes from different response structures between single stream queries and multiple stream queries. When you hit /streams with filters, the response structure completely changes from what you’re used to.
For filtered requests like /streams?game=GameName&limit=10, the _links object sits at the root level, not under streams. Try $selfUrl = $api_response[‘_links’][‘self’] instead. You’ll find self, next, and prev URLs all in that root _links object.
Side note - the Kraken API you’re using has been deprecated for ages. You should really migrate to the Helix API. It’s got consistent structure, better reliability, and proper auth headers.
Yeah, that’s exactly the issue - single stream calls nest everything under stream but filtered responses dump _links at the root. Twitch’s API is inconsistent like that.
Honestly, I’d just automate this instead of fighting with it manually. I built something similar for monitoring multiple streamers and game categories, and it was a game changer.
I set up automated workflows that handle all these API structure variations without me having to think about it. No more debugging different response formats or dealing with deprecated endpoints. It handles auth, rate limiting, and data extraction automatically.
You can build conditional logic that adapts to whatever structure Twitch throws at you, parse what you need, and transform everything into consistent formats regardless of which endpoint you’re hitting.
Saved me tons of time debugging API responses and dealing with Twitch’s weird inconsistencies. Plus you can scale it to monitor hundreds of streams without touching code.
Check out https://latenode.com for building these workflows.
yeah, kraken api is a pain with this. filters or multiple streams return different json than single stream calls. try $api_response['_links']['self'] instead of digging through the streams array. also, that old kraken endpoint’s officially dead - switch to helix api before it completely stops working.
You’re mixing up response structures. Your working code hits single stream endpoints that nest data under stream, but filtered responses use completely different paths. With multiple streams or filtered results, the self URL sits at $api_response['_links']['self'] - not under a streams key. Twitch loves using inconsistent response formats across different endpoints. I hit this same issue building stream monitoring tools. Always var_dump($api_response) first to see the actual structure before writing extraction code. Every endpoint type has its own quirks. Also, add proper error handling around that file_get_contents() call. Twitch API is unreliable and throws warnings when requests fail, which completely breaks JSON parsing.