将唯一的 JSON 缓存文件从 PHP 传递到 Javascript 以漂亮的格式显示

Pass Unique JSON cached file from PHP to Javascript to Display in a nice format

我已经苦苦挣扎了 2 天,试图弄清楚如何传递我的 PHP 抓取的唯一 json 文件,缓存它并(最终)随机播放 [=36] =]数据,只显示30条"games"。并将其放入我的 index.php 文件中,该文件具有 .ajax 调用。这样我就可以正确地格式化数据以供前端使用。

这是在 JSON 中发回给我的示例。

{
"response": {
    "game_count": 485,
    "games": [
        {
            "appid": 10,
            "name": "Counter-Strike",
            "playtime_forever": 32,
            "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5",
            "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f",
            "has_community_visible_stats": true
        },
        {
            "appid": 20,
            "name": "Team Fortress Classic",
            "playtime_forever": 0,
            "img_icon_url": "38ea7ebe3c1abbbbf4eabdbef174c41a972102b9",
            "img_logo_url": "515bc393c861d91b9165f0697040c015f50bcb5e",
            "has_community_visible_stats": true
        },
        {
            "appid": 30,
            "name": "Day of Defeat",
            "playtime_forever": 0,
            "img_icon_url": "aadc0ce51ff6ba2042d633f8ec033b0de62091d0",
            "img_logo_url": "beff21c4d29d2579e794c930bae599cd0c8a8f17",
            "has_community_visible_stats": true
        },
        {
            "appid": 40,
            "name": "Deathmatch Classic",
            "playtime_forever": 0,
            "img_icon_url": "c525f76c8bc7353db4fd74b128c4ae2028426c2a",
            "img_logo_url": "4bb69695ef9d0ae73e73488fb6456aa4ea1215fa"
        },
        {
            "appid": 50,
            "name": "Half-Life: Opposing Force",
            "playtime_forever": 52,
            "img_icon_url": "04e81206c10e12416908c72c5f22aad411b3aeef",
            "img_logo_url": "bb696ddfd8f8ef0892d5f2154d8617528d3cc2ec"
        },
        {
            "appid": 60,
            "name": "Ricochet",
            "playtime_forever": 58,
            "img_icon_url": "98c69e04cd59b838e05cb6980c12c05874c6419e",
            "img_logo_url": "f7a039402735c2dea82cbd412340873535e2be4c",
            "has_community_visible_stats": true
        },
        {
            "appid": 70,
            "name": "Half-Life",
            "playtime_forever": 63,
            "img_icon_url": "95be6d131fc61f145797317ca437c9765f24b41c",
            "img_logo_url": "6bd76ff700a8c7a5460fbae3cf60cb930279897d",
            "has_community_visible_stats": true
        },
        {
            "appid": 130,
            "name": "Half-Life: Blue Shift",
            "playtime_forever": 0,
            "img_icon_url": "b06fdee488b3220362c11704be4edad82abeed08",
            "img_logo_url": "927d38e83e3e4ca218a0fc7b32515eeb99aa8ff7"
        },
        {
            "appid": 80,
            "name": "Counter-Strike: Condition Zero",
            "playtime_forever": 0,
            "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236",
            "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f"
        },
        {
            "appid": 100,
            "name": "Counter-Strike: Condition Zero Deleted Scenes",
            "playtime_forever": 0,
            "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236",
            "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f"
        },
        {
            "appid": 220,
            "name": "Half-Life 2",
            "playtime_forever": 1010,
            "img_icon_url": "fcfb366051782b8ebf2aa297f3b746395858cb62",
            "img_logo_url": "e4ad9cf1b7dc8475c1118625daf9abd4bdcbcad0",
            "has_community_visible_stats": true
        },

steam.php

    if ($_SESSION['T2SteamAuth'] !== null) {

        $Steam64 = str_replace("http://steamcommunity.com/openid/id/", "", $_SESSION['T2SteamAuth']);
        $profile = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={$apikey}&steamids={$Steam64}");

        $buffer = fopen("cache/{$Steam64}.json", "w+");
        fwrite($buffer, $profile);
        fclose($buffer);


        $i            = 0;
        $imageUrl     = "http://media.steampowered.com/steamcommunity/public/images/apps/";
        $url          = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001?key={$apikey}&steamid={$Steam64}&include_appinfo=1&include_played_free_games=1";
        $json_object  = file_get_contents($url);
        $json_decoded = json_decode($json_object);
        $shuffle($json_decode)**[WIP.]**
        $buffer = fopen("cache/games/{$Steam64}.json", "w+");
        fwrite($buffer, $url);
        fclose($buffer);
        foreach ($json_decoded->response->games as $gamesList) {

            echo "
                <br/>Player ID: $gamesList->appid
                <br/>Player Name: $gamesList->name
                <br/>LargeAvatar: <img src=\"http://media.steampowered.com/steamcommunity/public/images/apps/$gamesList->appid/$gamesList->img_logo_url.jpg\" />";
            if ($i++ == 30)
                break;
        }

    }

    header("Location: index.php");
}

}




$steam = json_decode(file_get_contents("http://XXXXX/steam/cache/{$_SESSION['T2SteamID64']}.json"));
$gameList = json_decode(file_get_contents("http://XXXXX/steam/cache/games/{$_SESSION['T2SteamID64']}.json"));


echo $login;

echo "<img src=\"{$steam->response->players[0]->avatarfull}\"/>";
echo $steam->response->players[0]->personaname;

?>

index.php

$(document).ready(function(){
  $.ajax({  
      type: "GET",  
      url: "http://XXXXX/steam/steam.php",  
      data: "",  
      dataType: "json",  
      success: function(resp){  
        alert("success: "+resp);  
        var result = JSON.parse(resp);  

    $.each(result.response.games, function(i,games){

            content += '<p>' + games.appid + '</p>';
          content += '<p>' + games.name + '</p>';
                 content += '<img src="' + appid + imageUrl + games.img_logo_url + '.jpg' + '"/>';
               content += '<br/>';

             });
             $(content).appendTo("#posts");




      },  
      error: function(e){  
        alert("fail");  
      }  
    });  


 });
    $json_object  = file_get_contents($url);
    $json_decoded = json_decode($json_object, true);
    $games = $json_decoded['response']['games'];
    shuffle($games);

    $buffer = fopen("cache/games/{$Steam64}.json", "w+");
    fwrite($buffer, $url);
    fclose($buffer);
    foreach ($games as $gamesList) {

        echo "
            <br/>Player ID: ".$gamesList['appid']."
            <br/>Player Name: ".$gamesList['name']."
            <br/>LargeAvatar: <img src=\"http://media.steampowered.com/steamcommunity/public/images/apps/".$gamesList['appid']."/".$gamesList['img_logo_url'].".jpg\" />";
        if ($i++ == 30)
            break;
    }