KamiiQ
August 8, 2017, 1:35pm
1
Hello there, i find code on this forum and it wont works…
I changed userid, name etc.
<?php
$streamChannel = "kamiiq";
$url = "https://api.twitch.tv/kraken/streams?channel=" . $streamChannel;
$clientID = '6x31cwwskz31pmkfibymz8un59icd2';
function file_get_contents_curl($url) {
$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, $clientID);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$json_array = json_decode(file_get_contents_curl($url), true);
if(isset($json_array['streams'][0]['channel'])) {
echo '<span style="color:green">ONLINE</span>';
} else {
echo '<span style="color:red">OFFLINE</span>';
}
?>
One thing I see is that you’re not setting the Client-ID header properly. Check out the example in the Client-ID blog post and see if that helps.
1 Like
KamiiQ
August 8, 2017, 2:48pm
3
<?php
$streamChannel = "kamiiq";
$url = "https://api.twitch.tv/kraken/streams?channel=" . $streamChannel;
$clientID = '6x31cwwskz31pmkfibymz8un59icd2';
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
'Client-ID: ' . $clientId
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $channelsApi . $channelName
));
function file_get_contents_curl($url) {
$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, $clientID);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$json_array = json_decode(file_get_contents_curl($url), true);
if(isset($json_array['streams'][0]['channel'])) {
echo '( ͡° ͜ʖ ͡°)';
} else {
echo 'nope';
}
?>
its saying nope, always. (multistreaming with restream [twitch and yt work correctly]) i check app config, its correct - for me…
Your channel shows up in my PHP code (copied from the blog post and put it into a PHP file). The problem is with the code. You’re still setting the headers incorrectly from what I can tell. You’re calling curl_setopt_array
on the $ch
variable that doesn’t exist, which is likely your issue. I would recommend putting in some logging and testing from there.
1 Like
Alca
August 8, 2017, 4:09pm
5
Try putting curl_setopt_array
inside of the function after you define $ch
1 Like
KamiiQ
August 8, 2017, 4:19pm
6
You mean something like this?
<?php
$streamChannel = "kamiiq";
$url = "https://api.twitch.tv/kraken/streams?channel=" . $streamChannel;
$clientID = '6x31cwwskz31pmkfibymz8un59icd2';
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
'Client-ID: ' . $clientID
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $channelsApi . $streamName
));
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, $clientID);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$json_array = json_decode(file_get_contents_curl($url), true);
if(isset($json_array['streams'][0]['channel'])) {
echo '( ͡° ͜ʖ ͡°)';
} else {
echo 'nope';
}
?>
Not working, its my full code if someone needs this: https://pastebin.com/P4fMScT4
Alca
August 8, 2017, 4:43pm
7
The docs say that the value for CURLOPT_RETURNTRANSFER
and CURLOPT_HEADER
in curl_setopt
should be a bool
, not 0
or 1
.
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
And you’re also overwriting CURLOPT_RETURNTRANSFER
in curl_setopt_array
with the additional statements. I suggest combining them all into one curl_setopt_array
or individual curl_setopt
's for clarity and make sure to prune the duplicates.
1 Like
KamiiQ
August 8, 2017, 5:03pm
8
php code checker says “No issues found.”, code:
https://pastebin.com/1BuJbzr4
but its not working, always.
Alca
August 8, 2017, 5:18pm
9
You’ve put $ch = curl_init();
on the outside of the function – which probably isn’t an issue, but it doesn’t make sense.
1 Like
KamiiQ
August 8, 2017, 5:28pm
10
https://blacksquad.eu/ you can see here its not working but /stream.php display it correctly so stream is online (i dont want to show embed stream, only annoucement)
If it helps:
php 7.1
and im multistreaming with restream.
//ps. yes i moved it into function.
Alca
August 8, 2017, 5:47pm
11
Alright, I’ve actually tested and whatnot. Here’s what’s wrong:
You still have duplicate entries in curl_setopt_array
, namely CURLOPT_HTTPHEADER
.
CURLOPT_URL
adds $streamName
, an undefined variable, to the $url
which already has the channel name from its initialization.
Here’s the script I was working with that actually works:
<?php
$streamChannel = "kamiiq";
$url = "https://api.twitch.tv/kraken/streams?channel=" . $streamChannel;
$clientID = "6x31cwwskz31pmkfibymz8un59icd2";
function file_get_contents_curl($url, $headers) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE
]);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$headers = [
"Client-ID: " . $clientID
];
$data = file_get_contents_curl($url);
$json_array = json_decode($data, true);
if(isset($json_array["streams"][0]["channel"])) {
echo "( ͡° ͜ʖ ͡°)";
} else {
echo "nope";
}
?>
1 Like
KamiiQ
August 8, 2017, 5:53pm
12
Whoa, thank you bro. How to set your post as solution?
Alca
August 8, 2017, 5:55pm
13
I don’t believe that’s a thing on this platform.
1 Like
KamiiQ
August 8, 2017, 5:57pm
14
whoo i said it so fast. when im offline it’s showing my stream on site. https://pastebin.com/f60n4ws6
//okay i must wait a while before it changes - someone can set alca’s post as solved and/or close topic!
system
Closed
September 7, 2017, 5:57pm
15
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.