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

im trying to fix an online/offline system on my site but no matter what i do it will only show offline. can someone plx help me out …
this is what i use :

<?php
$channels = array("STREAMER") ;
$callAPI = implode(",",$channels);
$clientID = 'TWITCHID';
$arrContextOptions=array( "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false, ), );
$dataArray = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI . '&client_id=' . $clientID, false, stream_context_create($arrContextOptions)), true);
foreach($dataArray[‘streams’] as $mydata){
if($mydata[’_id’] != null){
$name = $mydata[‘channel’][‘display_name’];
$game = $mydata[‘channel’][‘game’];
$url = $mydata[‘channel’][‘url’];
echo “ONLINE”;
}
}
if($dataArray[‘streams’] == null or $dataArray[‘streams’] == “”)
{
echo	"No Streams Online" ;
}
?>

This looks like it was butchered from code written for checking several channels at once. I assume you are just trying to show if a single streamer is ONLINE/OFFLINE?

for the the test i tried it with one , but i get the same result with multiple.
the goal is to show multiple

Use cURL not file_get_contents

I predict the file_get_contents call is falling as on any sensible Web Host, file_get_contents is not allowed to make HTTP Requests

See the snippet below, you can use this with an array of channels of course… but in the line i marked you have grave quotes instead of single quotes. This is breaking your check, I believe… any how… I added an example that uses cURL like Barry suggested.

<?php
$channels          = array(
    "STREAMER"
);
$callAPI           = implode(",", $channels);
$clientID          = 'TWITCHID';
$arrContextOptions = array(
    "ssl" => array(
        "verify_peer" => false,
        "verify_peer_name" => false
    )
);
$dataArray         = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI . '&client_id=' . $clientID, false, stream_context_create($arrContextOptions)), true);
foreach ($dataArray[‘streams’] as $mydata) {
    if ($mydata[’_id’] != null) {
        $name = $mydata[‘channel’][‘display_name’];
        $game = $mydata[‘channel’][‘game’];
        $url  = $mydata[‘channel’][‘url’];
        echo “ONLINE”;
    }
}
if ($dataArray[‘streams’] == null or $dataArray[‘streams’] == “”) {     <===IN HERE
    echo "No Streams Online";
}
?>

When I first saw this I thought you were just trying to check a single channel, so I made this updated snippet… It uses cURL and v5 for checks but can be modified for any case.

<?php

function file_get_contents_curl($url) {
    $curlHeader = array(
        "Client-ID: xxxxxxxxxxxxxxxxxxxxxxxx",    /* SET CLIENT ID HERE */
        "Accept: application/vnd.twitchtv.v5+json"
    );
    $ch         = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeader);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function Login2ID($user) {
        $L2ID = json_decode(@file_get_contents_curl('https://api.twitch.tv/kraken/users?api_version=5&login=' . $user), true);
        $UserID = $L2ID['users']['0']['_id'];
        return $UserID;
}

$channelName = 'matt_thomas';   /* SET TWITCH NAME HERE */
$channelID = Login2ID($channelName);

$dataArray = json_decode(file_get_contents_curl('https://api.twitch.tv/kraken/streams?channel=' . $channelID), true);
if ($dataArray['streams']['0']['_id'] != null) {
    echo "Online";
}
else {
        echo "Offline";
}
?>

Of course you can work in the channel array and implode to use multiple channels.

1 Like

Should also consider adding a curl_getinfo and testing the http response code

And/or doing something more optimal to cache the data into a database for rate limiting reasons etc but sorta beyond the basic snippet

1 Like

Good info, I like to have some detailed examples/snippets. Thx

thnx so much for this got it to work with one streamer thnx
know time to find out how to fix the syntax error when adding one extra to the one you already placed

ok been trying all night but cant figure out what im doing wrong… everytime i add an extra streamer i get a syntax error about ‘,’, not correct… how can i fix that and is it ok that when the systems go live i refer to all the help here / give you all full credit for the fix ?

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

im trying to learn it with this project so im verry greatfull for al the help and feedback and actual explanation i get here about what i did wrong instead of instand fixes or no room to learn :smiley:
im trying serveral option right now including trying to make it read from a data but and this version as well
so far im trying to figure out the ‘$url’ (T_VARIABLE) error of this one

I omitted the ; in

<?php

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

In error. Trace the PHP error back.

1 Like

sweet got it to work thnx

now im working on how to hide the offline channels and i have te feeling i have to do that by changing the :
foreach ($channels as $channel) { part.
so far i got this but cant get it to read $channel for the small video preview

<?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:  qb04sm1wihjshxsrejejv5g053y4r0"
]);
$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 '<iframe src="https://player.twitch.tv/?channel= $channel" frameborder="0" allowfullscreen="true" scrolling="no" height="200" width="200"></iframe>';
            }
        } else {
            echo 'No Streamers Online';
        }
    } else {
        echo 'Helix Error (No Data)';
    }
} else {
    echo 'Non 200 ' . $info['http_code'] . ' - ' . $data;
}
?>
<?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:  qb04sm1wihjshxsrejejv5g053y4r0"
]);
$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) {
            foreach ($data->data as $stream) {
                echo '<iframe src="https://player.twitch.tv/?channel=' . strtolower($stream->user_name) . '" frameborder="0" allowfullscreen="true" scrolling="no" height="200" width="200"></iframe>';
            }
        } else {
            echo 'No Streamers Online';
        }
    } else {
        echo 'Helix Error (No Data)';
    }
} else {
    echo 'Non 200 ' . $info['http_code'] . ' - ' . $data;
}
1 Like

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