Get current active team name and link from a spesific channel

Hello. I’m not at all a programmer, but I’m trying to learn how to get around the API to pull some channel information to be used in my overlay and website. So far I’ve been able to pull things like current game, status, number of live viewers, channel views and the follower number and implemented that by finding some code to trial and error with. However, I can’t seem to figure out how to pull the current active channel team name (and link). Below is how I’ve tried to do it so far, I guess I’ve not at all figured out how the API works.

If anyone could explain it to me in simple terms what I’m doing wrong that would be fantastic, thank you.

// Twitch username
define("TWITCHCHANNEL", "lirik");

// Current active team in channel on Twitch
function twitchTeam() {
  $data = json_decode(@file_get_contents('https://api.twitch.tv/kraken/channels/' . TWITCHCHANNEL . "/teams/"), true);
  $team = $data['_links']['self']['teams']['_links']['display_name'];

  if($team != null)
  {
      echo $team;
  }
  else
  {
      echo "API error";
  }
}

The link you should be using is this:

https://api.twitch.tv/kraken/teams

Look over this doc:

https://github.com/justintv/Twitch-API/blob/master/v3_resources/teams.md

For a users teams, use this:

https://api.twitch.tv/kraken/channels/lirik/teams (note that there’s no trailing /)

Also, not sure why you’re specifying the team as being in (message data) (links) (self) (teams) (links) (displayname), just take the top level object, look at ‘teams’, ‘name’

  • Links
    – Self
  • Teams
    – _id
    – name
    – info
    – display_name
    – created_at
    – updated_at
    – logo
    – banner

There are a couple of things here. I would recommend learning how to use cURL instead of file_get_contents. In August, we’re requiring that developers send the Client-ID on their request and file_get_contents can’t add that header properly. This thread should help with learning how to use cURL.

For your other question, you’re indexing into something that doesn’t exist here:

$team = $data['_links']['self']['teams']['_links']['display_name'];

If you read that, you’re looking into the response data for the _links property with the self property with the teams property, etc. etc. Now, compare that to the documentation here. In those docs, it shows that the API response has a teams array with an object inside that has the display_name. You’re trying to access a property that doesn’t exist.

Thank you pointing out the cURL thing, I wasn’t aware that was required when Client-ID kicks in, hopefully i’ll figure out how to rewrite that.

As for the current code, I’ve been experimenting, thinking I’ve been reading it wrong. When I try: $team = $data['_links']['self']; I actually get the expected result, but when I try: $team = $data['teams']['display_name']; I get nothing and it just returns API error. So I’ve been experimenting a bit with: $data = json_decode(@file_get_contents('https://api.twitch.tv/kraken/channels/' . TWITCHCHANNEL . "/teams"), true); trying things like /channels/ or /teams/ or even /channels/lirik/teams, (thanks @tournymasterbotCurse), to see if it’s that url that is wrong, so far no luck there.

Updated code so far:

// Current active team on Twitch
function twitchTeam() {
  $data = json_decode(@file_get_contents('https://api.twitch.tv/kraken/channels/' . TWITCHCHANNEL . "/teams"), true);
  $team = $data['teams']['display_name'];

  if($team != null)
  {
      echo $team;
  }
  else
  {
      echo "API error";
  }
}

You are getting API error because that’s what you’ve told it to do when $team is null. Because you’re looking directly at teams-> display_name you won’t see the error message returned from twitch, which is probably an unauthorized message, since you don’t appear to be passing a valid client id header.

Try echoing $data

To be honest I don’t know exactly what that NULL is as I’ve just been editing some code I’ve found.

Echoing $data returns the word “Array”. Echoing $team returns nothing/blank. I’m not getting any errors of any kind. It could be that MAMP is not displaying any errors for some reason? From what I can tell, Error reporting seems to be E_ALL and display reporting seems to be On.

No I’m not passing any Client-ID at this point, is that required for this call? If that’s the case that should be my next step, it’s something I need to do anyway, though, that’s a completely different problem for me to solve.


Edit:
Actually, there was an issue with MAMP, errors are now showing. Let me try something.

Edit 2:
Here are the errors when echoing $data.


Warning: twitchTeam(): It is not safe to rely on the system’s timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC’ for now, but please set date.timezone to select your timezone. in URL removed on line 139

Notice: Undefined index: display_name in URL removed on line 139

Warning: twitchTeam(): It is not safe to rely on the system’s timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC’ for now, but please set date.timezone to select your timezone. in URL removed on line 141

Notice: Array to string conversion in URL removed on line 141

In the context of programming, objects have two states where null = uninitialized (the other state is initialized) - when an object is null it means that it basically does not exist in any way. When you use JSON, if the object you are trying to deserialize is not formatted correctly most serializers will set the object to null instead of throwing an error.

In general, with where you are at right now I would search the following on google:

[Your language> - null handling
[Your Language> - Web Request
[Your Language> - Deserialize JSON
[Your Language> - Echo Object Values

From your error, your $team = blahblah is probably line 139, I suspect $data is null.

Yeah line 139 is $team = $data['teams']['display_name']; and line 141 was set to echo $data; instead of the conditional statement that was there above.

I will google those suggestions, thank you very much.

Please be aware that the teams object is an array of JSON objects. So, you have an array (teams) within an array ($data). I would try something like this to see if you’re getting data back properly:

$teams = $data['teams'];
echo $teams[0];

That returned the word “Array”.

It would be $teams = $data[‘teams’][0][‘display_name’]

Oh that actually returned a team name. I was expecting the users current active team though, is this the latest team joined if multiple?

It’s a list of the users teams, that might be one, or 10, or a million and five. The user only shows one team, but you have no way of knowing which is selected for display (from this endpoint). You can use a for loop to iterate over all the users teams.

I’m sorry if I’ve been unclear. I’m only interested in the one the user has selected for display.

You can’t get that from the api.

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