PHP Online/Offline using the Twitch API

There are a Few errors here so I’ll list them

  1. You are double json_decoding, the second call is unneeded
  2. The second call to json_decode you are decoding '$twitch_api' this decodes the string $twitch_api and fails, you need json_decode($twitch_api)
  3. 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.
  4. When using curl check the HTTP Response is a 200
  5. if ($twitch_api->stream == null) will suffice for your online/offline check
  6. 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)
1 Like