Logo Issues w/ API

I did make another post about getting offline/online to show up, which has been resolved, however I figured best to open a new topic since this is different than the other one I made. Anyways … I am still working on trying to get this to work like I want to.

http://pastebin.com/d1ZcN36E

On there I have the full code that I have along with at the top comments on what I am wanting for it to do and what it actually IS doing. I’m hoping to get some help with this since I’m not sure what to do to get this to work how I’m looking to get it work. I’m new-ish on working with the twitch api for all this.

Thanks <3

        foreach ($channels as $name) {
            $live[$name] = null;
            $chanLogo     =     "https://api.twitch.tv/kraken/channels/$name";
            $chandata     =     file_get_contents($chanLogo);
            $result         =     json_decode($chandata);
            $offlogo     =    $result->logo;
            }

Plus this in the echo:

$offSrc = file_exists($offlogo) ? $offlogo : “twitch-default.jpg”;

now shows the default image, however now it’s not showing the channel logo at all if there is a logo that actually exists. And still using the image of the last on the list for all streams. Progress but I’m still getting stumped.

Are you sure your file_get_contents is working.

Please use cURL most systems block file_get_contents being able to make HTTP/S requests. So please use cURL and retry. You should also be testing to see if you get a HTTP 200 when you make a request to the channel.

Furthermore make sure that $name is in lowercase.

yea I just noticed that it’s not working right when I tried to swtich the list around. When I placed a online channel on the bottom with no logo set, it was a broken image. I’m still not seeing why it’s picking up the last on the array and using that picture for every other stream either.

http://icefaerie.org/twitch/

subspace radio and eve radio both have logo’s set, but not displaying them, they are displaying the default logo that is set in the ‘file exists’ - channels that don’t have logo’s are still broken, and what I stated above (using same image).

all my names are lowercase but made no difference.

my system isn’t blocking the file_get_contents, so that’s not the issue.

if I take out the file exists in the live channels, they show up, and the ones that don’t have a channel logo are broken (since they have no logo set)

ok, so the part that is causing the loop of the same last logo/name is this:

        foreach ($channels as $name) {
            $live[$name] = null;
            $chanlogo     =     "https://api.twitch.tv/kraken/channels/$name";
            $chandata     =     file_get_contents($chanlogo);
            $result         =     json_decode($chandata);
            $offlogo     =    $result->logo;
            $oname         =     $result->display_name;
            }

But in order to have the offline display names and logo’s I need to use that api from /channels not /streams, is there a way to break this up so it isn’t looping like it is now and pulling the same thing for all channels on the list?

This is the full code:

<?php
$live = array();
$channels = array("channel1", "channel2", "channel3",);

$streamdata = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true);

            foreach ($channels as $name) {
                $live[$name] = null;
                $chanlogo     =     "https://api.twitch.tv/kraken/channels/$name";
                $chandata     =     file_get_contents($chanlogo);
                $result         =     json_decode($chandata);
                $offlogo     =    $result->logo;
                $oname         =     $result->display_name;
                }

            foreach ($streamdata["streams"] as $stream)  {
                $live[$stream["channel"]["name"]] = $stream;    
            }
            
            foreach ($live as $name => $stream) {
                    if($stream != null){
                                    $dname         =     $stream['channel']['display_name'];
                                    $game         =    $stream['game'];
                                    $logo         =    $stream['channel']['logo'];
                                    $viewers     =    $stream['viewers'];
                            echo "<p><img src='$logo' height='45' width='45' style='z-index: 5;float:left;PADDING-RIGHT: 3px;PADDING-LEFT: 3px;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$dname</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='online.png' height='7' width='7'> $game<br>Viewers: $viewers</font></p>";
                            } else{    
                                echo "<p><img src='$offlogo' height='45' width='45' style='z-index: 5; float:left;PADDING-RIGHT: 3px;PADDING-LEFT: 3px;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$oname</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='offline.png' height='7' width='7'> Offline<br><br></font></p>";
                            }
            }                     
?>

You are reassigning the variables in the first foreach loop for each iteration. You need to assign the new values as properties of each channel… I think…

Was some time since I did any php.

i tried to create a new foreach loop but that didn’t do anything :confused:

After a rewrite of my original code, it works now :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.