dpa84373
dpa84373
2014-07-22 06:10

将PHP集成到JavaScript中以获取JSON

  • json
  • php
  • javascript

I installed PHP5 and Apache2 on an Linux vServer (8 Gb Ram ; 8 vCores) and I was scripting but I can't get rid of an error in my code. I'm trying to fetch JSON with integrated PHP but it doesn't work. Here is part of the source code:

function GameDetails(servername, serverurl, mapname, maxplayers, steamid, gamemode) {
    var data = '<?php header("access-control-allow-origin: http://api.steampowered.com");$jsonData = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=*REMOVEDREMOVEDREMOVED&steamids=76561197960435530');echo $jsonData;?>';
    document.getElementById('server_name').innerHTML = jsonObj.response.players[0].personaname;
});
}

And here is the version after PHP Processing:

function GameDetails(servername, serverurl, mapname, maxplayers, steamid, gamemode) {
    var data = '{
        "response": {
            "players": [
            {
                "steamid": "76561197960435530",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "Robin",
                "lastlogoff": 1405647102,
                "profileurl": "http://steamcommunity.com/id/robinwalker/",
                "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4.jpg",
                "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_medium.jpg",
                "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_full.jpg",
                "personastate": 0,
                "realname": "Robin Walker",
                "primaryclanid": "103582791429521412",
                "timecreated": 1063407589,
                "personastateflags": 0,
                "loccountrycode": "US",
                "locstatecode": "WA",
                "loccityid": 3961
            }]
        }
    }';
    alert data;
    document.getElementById('server_name').innerHTML = data.response.players[0].personaname;
});
}

I know its a loading screen for a Garrys Mod Server. The Debug Console from Firefox says:

SyntaxError: unterminated string literal.

But it's just normal JSON. JSON examples from web works without errors.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐