List of active streams using Twitch API & PHP

Hello, I want to get the output of the streamer via API, if he is online. I have read past articles and usually the code:

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

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

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

    $client_id = "***";
    $token = "***";

    $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';
        }
    }
?>

I can’t understand why it doesn’t work. help me please

Whats the error you are getting?

Is $token a valid access token for the API?

Which echo line is echo-ing?

Status 500 Internal Server Error

That’s not an OAuth token, that’s the Client Secret.

You need to go through the Authentication Docs and generate an Access Token to make API requests.

<?php
$client_id = "************";
$client_secret = "*******";

$link = "https://id.twitch.tv/oauth2/token";
$data = "client_id=" . $client_id . "&client_secret=" . $client_secret . "&grant_type=client_credentials";

// Request cURL POST pour get le token
$ch = curl_init($link);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$res = curl_exec($ch);
curl_close($ch);

// Decode
$token = json_decode($res);

$names = Array("agentwxo", "welovegames");

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

$url = "https://api.twitch.tv/helix/streams?user_login=" . $names[1];

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token->access_token, 'Client-ID: ' . $client_id));
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 . '&parent=clicgo.ru&muted=true"
      height="480"
      width="720"
      allowfullscreen>
  </iframe>
  <iframe id="twitch-chat-embed"
          src="https://www.twitch.tv/embed/' . $name . '/chat?parent=clicgo.ru"
          height="480"
          width="350">
  </iframe>';
      } else {
        echo $name . ' not live';
    }
}

?>

I can not understand where the error is, Visual Studio code does not give an error. And the site doesn’t work.

What is being echo’ed/outputted when you run this?

Plugging my own ClientID/ClientSecret into the script

It ran successfully.

thank you very much, it was the php version, it was old

final working code

<?php
$client_id = "*****";
$client_secret = "*****";

$link = "https://id.twitch.tv/oauth2/token";
$data = "client_id=" . $client_id . "&client_secret=" . $client_secret . "&grant_type=client_credentials";

// Request cURL POST pour get le token
$ch = curl_init($link);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$res = curl_exec($ch);
curl_close($ch);

// Decode
$token = json_decode($res);

$names = array(
    "agentwxo",
    "digitizedperson",
);

$found = false;

foreach ($names as $name) {
    $url = "https://api.twitch.tv/helix/streams?user_login=" . $name;

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token->access_token, 'Client-ID: ' . $client_id));
    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) {

            if (!empty($result->data)) {
                if ($result->data[0]->type == 'live') {
                    echo '<iframe
                           src="https://player.twitch.tv/?channel=' . $name . '&parent=clicgo.ru&muted=true"
                           height="480"
                           width="720"
                           allowfullscreen>
                       </iframe>
                       <iframe id="twitch-chat-embed"
                               src="https://www.twitch.tv/embed/' . $name . '/chat?parent=clicgo.ru"
                               height="480"
                               width="350">
                       </iframe>';
                    $found = true;
                    break;
                }
            }

            // break;

        } else {
            echo 'Failed to decode JSON';
        }
    } else {
        echo 'Failed with: "' . $info['http_code'] > '"';
    }
}

if (!$found) {
    echo '
    <div style="height: 100vh; width: 100%;  position: relative;">
    <div style="margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);"><img src="https://clicgo.ru/twitch/White-Anthropomorphic-Cat.svg" width="250" alt=""><center><p style="color:white">Strean not found <br></p></center></div> 
</div>
    ';
}

?>

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