Jump to content

Echo does not work in else statement.


Sydth

Recommended Posts

I am using the Twitch API to tell if a user is live. I'm new to the Twitch API and PhP so please forgive me if I make any mistakes. Expand the Spoiler tags below to see the code. The code below SHOULD be telling me if a channel is not live or not. It tells me if they are live, but not if they are not live. I don't understand.

 

 

 

<?php

    if (!empty($_GET['channel']))

    {

        $channel = $_GET['channel'];

        $dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel='.$channel), true);

        

        foreach ((array) $dataArray['streams'] as $mydata)

        {

            if (!empty($mydata['_id']))

            {

                echo "Channel is live<br><br>";

                

                $uptime = $mydata['created_at'];

                $uptime = str_replace("Z", "UTC", $uptime);

                $uptime = preg_replace("/T/", "", $uptime, 1);

                echo "Twitch Uptime: ".$uptime."<br />";

                

                date_default_timezone_set('UTC');

                $localUptime = date('Y-m-dh:i:sT', time());

                echo "UTC Local Time: ".$localUptime;

            }

            else

            {

                echo "Channel is not live";

            }

        }

    } else {

        echo "No channel";

    }

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/
Share on other sites

Am I correct in saying that an _id will always exist?

I don't believe that determines being live or not.

 

Looking at the api I see...

 

"stream": null,

 

So check if steam is null to determine offline.

 

https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md

It shows it like so, is what I would try.

 

If offline

{
  "stream": null,
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/test_channel",
    "channel": "https://api.twitch.tv/kraken/channels/test_channel"
  }
}

 

It shows it like so, is what I would try.

 

If offline

{
  "stream": null,
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/test_channel",
    "channel": "https://api.twitch.tv/kraken/channels/test_channel"
  }
}

I use the older version of the API, not the newer one.

 

It shows it like so, is what I would try.

 

If offline

{
  "stream": null,
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/test_channel",
    "channel": "https://api.twitch.tv/kraken/channels/test_channel"
  }
}

Okay, so I've converted over to the newer API. Thanks a lot!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.