Jump to content

Echo does not work in else statement.


Go to solution Solved by QuickOldCar,

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

Edited by QuickOldCar
  • Like 1
  • Solution

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"
  }
}
Edited by QuickOldCar

 

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.