There are a Few errors here so I’ll list them
- You are double json_decoding, the second call is unneeded
- The second call to json_decode you are decoding
'$twitch_api'this decodes the string$twitch_apiand fails, you needjson_decode($twitch_api) - You should use curl instead of file_get_contents, file_get_contents is a security reason for HTTP calls, further more there is no way to check the HTTP Status code of the response.
- When using curl check the HTTP Response is a 200
-
if ($twitch_api->stream == null)will suffice for your online/offline check - Avoid what you are trying to do (start processing a object then start processing the same data as an array you can iterate on the object only)