The following if
always return "Stream is Online!"
, even when "Stream is Offline!"
Could anyone point me the error?
<?php
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Client-id: clientidhere
" . "Accept: application/vnd.twitchtv.v5+json
"
)
);
$context = stream_context_create($opts);
$file = file_get_
contents('https://api.twitch.tv/kraken/streams/somestreaming', false, $context);
$json = json_decode($file);
if (($json->stream === "NULL") || ($json->stream === "null")) {
echo "Stream is Offline!";
} else {
echo "Stream is Online!";
}
?>
For me, I write as follows.
if (empty($http_response_header)){
echo "Stream is Offline!
";
} else {
echo "Stream is Online!
";
}
Note: $http_response_header
is not empty when first request was succeeded then second request was failed, so you should need to empty $http_response_header
before each requests.
$http_response_header = '';
file_get_contents(...);