first off, try checkin what the api actually returns. instead of file_get_contents, use curl, it’s more reliable. like this: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch). then var_export($data) to see the structure.
Your loop syntax is fine, but you’re missing JSON validation. Had this exact problem with Twitch’s API last year. What happens is json_decode fails silently and returns null, then your foreach tries to loop over nothing. Add error checking right after decode: if (json_last_error() !== JSON_ERROR_NONE) { die(‘JSON decode failed’); }. Also, most streaming APIs need auth headers even for public stuff - file_get_contents might be getting a 401 instead of real data. Check if you need an authorization token or user-agent header, or you’re probably just getting an error message wrapped in JSON.
Most streaming APIs don’t return flat arrays - they wrap data in structured responses like {“data”: [{“username”: “user1”}, {“username”: “user2”}]}. First, add var_dump($data); right after json_decode to see what you’re actually getting. Once you know the structure, you’ll probably need foreach ($data[‘data’] as $subscriber) { echo $subscriber[‘username’]; }. Don’t forget error handling - check if json_decode worked with if ($data === null) { // handle error } since API calls fail sometimes.
The structure issue is your main problem, but check something else too. Streaming APIs often paginate responses even when you request 100 items - you might only get 20-50 per call. Happened to me pulling subscriber data for a client. The docs said 100 max but it actually capped at 25.
After you fix the JSON structure with var_dump, look for ‘next_page’ or ‘has_more’ fields in the response. You’ll probably need multiple API calls to get all 100 followers. And definitely cache the results - hitting the API on every page load will get you rate limited fast.
You’re probably trying to loop over the main response object instead of the actual subscriber array. Most streaming APIs wrap the data - you’ll get something like {“subscribers”: […]} or {“users”: […]} instead of a direct array. Dump the data first with print_r($data) to see what you’re actually working with. Then adjust your loop to something like foreach ($data[‘subscribers’] as $subscriber) { echo $subscriber[‘username’]; }. Also, add some basic checks - make sure $response isn’t false and $data isn’t null before you start looping. Trust me, I’ve been burned by API timeouts and bad responses way too many times.
Yeah, it’s the JSON structure causing problems. But don’t debug this manually every time - automate it.
I built something similar for pulling follower data from multiple platforms. Manual approach gets messy fast with rate limits, auth tokens, and different API formats.
What worked: automated workflow that handles API calls, parses JSON correctly, and stores everything in a database. You don’t need tons of PHP for this.
The workflow grabs data, detects JSON structure automatically, pulls usernames no matter how the API wraps them, and spits out clean HTML. No more guessing array keys or handling API failures by hand.
Set it on a schedule and your follower list updates itself. Way better than running the same script repeatedly.
Latenode’s great for this kind of automation - handles all the API complexity for you.