1

Okay, so I think you get what I wanna do by just watching the code.

//Get JSON text file (Steam API)
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');

//Decode JSON
$game_json = json_decode($json, true);

//Target game name and echo it
echo $game_json['name'];

The JSON itself comes in this order (unstructured, very sorry):

{"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"

So my target is ""name":"Tropico 4: Steam Special Edition"", which is what I want to echo on my page. I'm not sure if it helps, but "name": appears once, is something like [0] needed in my code to target the first? Is the nesting what's stopping me here, or is the $game_json['name']; an incorrect way of targeting?

ANY tips and/or help will be much appreciated. Thanks.

Algernop K.
  • 477
  • 2
  • 19

2 Answers2

3

In the future, use print_r($game_json) to check the array structure.

<?php
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');
$game_json = json_decode($json, true);
echo $game_json['57690']['data']['name'];
//Tropico 4: Steam Special Edition
echo $game_json['57690']['data']['required_age'];
//0
//etc...
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Thank you very much. You are much too kind. A little bit new to this, even the easiest of tutorials (the duplicate mark, for instance) are too confusing. – Algernop K. May 03 '16 at 22:10
  • You're very welcome @JohnSmith. You'll get there! persistence is the key :) – Pedro Lobito May 03 '16 at 22:12
  • If my answer helped you, please mark it as the correct answer, thank you ! – Pedro Lobito May 03 '16 at 22:13
  • Of course, but can't do it instantly, waiting for a timer! – Algernop K. May 03 '16 at 22:15
  • How do I make API text readable? Structured, not a long series of text. Tools? – Algernop K. May 03 '16 at 22:15
  • can you please elaborate further ? – Pedro Lobito May 03 '16 at 22:16
  • 1
    @JohnSmith Mark this as the answer. Don't hold him hostage while you ask completely irrelevant, random questions in the comments section. – Nate I May 03 '16 at 22:23
  • @PedroLobito Well it's just one long text on the same row. Any good idea on how to view it with indents and spaces? Any recommended tools? – Algernop K. May 03 '16 at 22:41
  • @NateI Ohhhh no, sorry if it appeared that way. The "best answer" was coming, was just waiting for the timer (I think it's 10 mins) so that I actually could select a best answer at all. Didn't wanna make another post here for a stupid question, figured that Pedro might know something about it. – Algernop K. May 03 '16 at 22:41
2
<?php

//This is your json string
$string = {"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"...

//Turn JSON string into object
$data = json_decode($string);

//Turn your object into an array (easier to work with in this case)
$data = (Array)$data;

//Get name of item with "57690" key
$name = $data["57690"]->data->name;

//Echo the name
echo($name);

//You can also echo out all names of all items like this:
foreach($data as $key => $item)
{
    echo($item->data->name);
}
ChrisA
  • 293
  • 2
  • 8
  • Thank you, I think I get the concept now! While I'm typing I might aswell ask, how do I structure this API so it's readable? It's just one long series of text looking in the browser.. – Algernop K. May 03 '16 at 22:13
  • 1
    depending on your editor, you may have built-in features or plugins available to auto-format JSON. I use Notepad++ and there is a plugin called "JSON Viewer" that auto-formats for me. – ChrisA May 03 '16 at 22:15
  • Perfect, just what I'm looking for. This community is too scrub-friendly. Thanks. – Algernop K. May 03 '16 at 22:45