What am I doing wrong? Stream Status JS

I’m not sure what I am doing wrong. I added my client-id and token but I still cant get this to work.
I am so unsure how to use the API anymore and don’t fully understand what’s going on. Can someone please help me fix this?

  <?php
    $names[] = "monstercat";

    $status = [];
    foreach ($names as $name) {
        $status[$name] = false;
    }

    $url = "https://api.twitch.tv/helix/streams?user_login=" . implode('&user_login=', $names);

    $client_id = "XXXXXXXXXXXX";
    $token = "XXXXXXXXXXXX";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Client-ID: ' . $client_id,
        'Authorisation: Bearer ' . $token
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    if ($info['http_code'] == 200) {
        $result = json_decode($result);
        if (json_last_error() == JSON_ERROR_NONE) {
            foreach ($result->data as $stream) {
                // do stuff with stream
                $name = strtolower($stream->user_name);
                $status[$name] = true;
            }
        } else {
            echo 'Failed to decode JSON';
        }
    } else {
        echo 'Failed with ' . $info['http_code'];
    }

    foreach ($status as $name => $state) {
        if ($state) {
            echo '<iframe src="https://player.twitch.tv/?channel=' . $name . '" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>';
        } else {
            echo $name . ' not live';
        }
    }
?>

Code Updated.

Please read the reference for the Get Streams endpoint Reference | Twitch Developers as you have several important things wrong.

There’s no need to make 1 request for each streamer. The Get Streams endpoint supports up to 100 streamers per request.

That’s not the correct URL, you don’t just append the streamers username as a path to the end of the URL. You need to user the user_login querystring param.

First, don’t EVER share an OAuth token. They should be treated as like passwords and kept private, and to share them is a violation of the Developer Agreement. I’ve gone ahead and revoked the token you shared.

Secondly, as the docs show the header is Authorization, not token, and the OAuth token needs to be prefixed with Bearer when using Helix endpoints.

Finally, none of your logic seems to actually be parsing the results. The response body will contain an object, within which is a data array containing each stream object (if they’re online). You need to check within that array which streams are returned and make any modifications to your page based on that.

wow, I didnt realize I left the token lol. resetting that now lol.
Man this used to be so simple to pull off. Getting the stream status used to be easy and now its just ugh. Most of this seems like an alien language now. Is there anywhere with a copy and paste method that I only need to input my token and id?

Does the above code now look better and up2date or need changes cause Its not working. If not working then I suspect its my ID and token.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.