-1

I'm a beginner in PHP and i am working with the steamapi. I got it figured out a bit and wanted to continue importing my current game in my site. Not i had the problem steam doesn't give anything when i use the steamapi and i'm not in game. When i am in game the code works fine (thats what i think thats going on).

To explain me a bit more: When i am in game: echo $json["response"]["players"][0]["gameextrainfo"]; --> this gives me the name of the game i am now in .

When i'm not in a game and i use: echo $json["response"]["players"][0]["gameextrainfo"]; --> i get this Notice:

Undefined index: gameextrainfo in C:\xampp\htdocs\steam\steamapi.php on line 10

So i think the problem i am having is that when i'm not in a game steam returns nothing?

Can someone help me with this?

Like i said i'm just a beginner in PHP so if this is a dumb question i'm sorry.

Cyber_Star
  • 155
  • 1
  • 9
  • 1
    Undefined index means that the array at $json['response']['players'][0] does not contain a key 'gameextrainfo'. Which makes sense, you're not playing a game, so no extra info is given. You can check if a certain key is present using `array_key_exists("gameextrainfo", $json["response"]["players"][0])` or `isset($json["response"]["players"][0]["gameextrainfo"])` – thisisboris Feb 15 '17 at 16:59
  • @thisisboris so if i understand right this will give 1 if i'm in game and 0 if i'm not? – Cyber_Star Feb 15 '17 at 17:06
  • @thisisboris thank you so mutch this is what i searched for! Please provide your comment in an answer so i can accept it! :) – Cyber_Star Feb 15 '17 at 17:19

2 Answers2

0

The steam api docs are at: https://developer.valvesoftware.com/wiki/Steam_Web_API

A pretty quick search under get player summaries returns:

gameextrainfo If the user is currently in-game, this will be the name of the game they are playing. This may be the name of a non-Steam game shortcut.

API documentation can be pretty hard to parse when you are first starting out, but generally it is the best place to go for information. Hope that helps.

--EDIT-- In cases where you are not in game the information will not be returned, to account for this you should add in a check for the data before attempting to print it.

// Set variable for individual player
$playerInfo = $json['response']['players'][0];

// Test if extra info exists in player info array
if (array_key_exists('gameextrainfo', $playerInfo) 
  && !empty($playerInfo['gameextrainfo'])
) {
  // echo info string
  echo $playerInfo['gameextrainfo];
}
D Lowther
  • 1,609
  • 1
  • 9
  • 16
  • This was the site what i was using but i couldn't figure it out what to do if the steamapi returned nothing. – Cyber_Star Feb 15 '17 at 17:07
  • ahh, I didn't understand your question it sounded like you weren't sure whether you should be getting data or not. – D Lowther Feb 15 '17 at 17:11
0

Undefined index means that the array at $json['response']['players'][0] does not contain a key gameextrainfo.

Which makes sense, you're not playing a game, so no extra info is given.

You can check if a certain key is present using

array_key_exists("gameextrainfo", $json["response"]["players"][0])

PHP docs

or

isset($json["response"]["players"][0]["gameextrainfo"])

PHP docs

In both cases it's assumed that there actually is a first player. You should check the steam-api to see which fields are always passed / always exist in the response.

thisisboris
  • 431
  • 1
  • 3
  • 13