Sydth Posted November 9, 2014 Share Posted November 9, 2014 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/ Share on other sites More sharing options...
QuickOldCar Posted November 9, 2014 Share Posted November 9, 2014 You want to check for if it's not null instead. if (!empty($mydata['_id'])) to if ($mydata['_id'] != null) Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496138 Share on other sites More sharing options...
hansford Posted November 9, 2014 Share Posted November 9, 2014 Find out what values are set if the channel is "not live". According to the manual: empty() "A variable is considered empty if it does not exist or if its value equals FALSE." Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496139 Share on other sites More sharing options...
Sydth Posted November 9, 2014 Author Share Posted November 9, 2014 You want to check for if it's not null instead. if (!empty($mydata['_id'])) to if ($mydata['_id'] != null) Same result. I had this before. Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496140 Share on other sites More sharing options...
QuickOldCar Posted November 9, 2014 Share Posted November 9, 2014 (edited) 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 November 9, 2014 by QuickOldCar 1 Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496141 Share on other sites More sharing options...
Sydth Posted November 9, 2014 Author Share Posted November 9, 2014 (edited) Am I correct in saying that an _id will always exist? I don't believe that determines being live or not. No. When a channel is not live When a channel is live Edited November 9, 2014 by Sydth Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496142 Share on other sites More sharing options...
Solution QuickOldCar Posted November 9, 2014 Solution Share Posted November 9, 2014 (edited) 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 November 9, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496143 Share on other sites More sharing options...
Sydth Posted November 9, 2014 Author Share Posted November 9, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496144 Share on other sites More sharing options...
Sydth Posted November 9, 2014 Author Share Posted November 9, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496145 Share on other sites More sharing options...
hansford Posted November 9, 2014 Share Posted November 9, 2014 (edited) Try checking is the streams array is empty to start with: then add an IF statement to see if the ID exists. But if the new API is working for you - great. Edited November 9, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/292369-echo-does-not-work-in-else-statement/#findComment-1496146 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.