Can some help me / eplain to me what i did wrongh?

That’s because you didn’t read the Docs or seem to have a working knowledge of PHP

Changing:

$channelName = 'matt_thomas'

to

$channelName = 'matt_thomas','another'

Won’t work as that’s invalid PHP, which is what I assume you did. Given the error you stated.

Additionally the provided code sample will not complete the stream lookup since it is not looping and can only look up one stream.

Something like the following using helix instead

Will let you lookup up to 100 streams without performing any cURL loops

(Not tested it jsut threw it together this is pre coffee code. It has some gotchas in it with dealing with non English usernames, but that is a different story. I would NOT use this in production.

<?php

$channels = [
    'username_1',
    'username_2'
]

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

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Client-ID: xxxxxxxxxxxxxxxxxxxxxxxx"
]);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

if ($info['http_code'] == 200) {
    $data = json_decode($data);
    if ($data->data) {
        if (count($data->data) > 0) {
            $live = array();
            foreach ($data->data as $stream) {
                $live[] = strtolower($stream->user_name);
            }
            foreach ($channels as $channel) {
                echo $channel . ' is ' . (array_search($channel, $live) !== false ? 'online' : 'offline');
            }
        } else {
            echo 'No One is online';
        }
    } else {
        echo 'Helix Error (No Data)';
    }
} else {
    echo 'Non 200 ' . $info['http_code'] . ' - ' . $data;
}
2 Likes