문제

So I've read a couple of topics on reading XML with simpleXML, but I'm having troubles with my specific case.

Currently I am trying to extract single values from this XML block that comes from a Steam web API URL

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE response>
<response>
  <players>
    <player>
        <steamid>76561197996635228</steamid>
        <communityvisibilitystate>3</communityvisibilitystate>
        <profilestate>1</profilestate>
        <personaname>P337</personaname>
        <lastlogoff>1386990920</lastlogoff>
        <commentpermission>1</commentpermission>
        <profileurl>http://steamcommunity.com/id/P337/</profileurl>
        <avatar>http://media.steampowered.com/steamcommunity/public/images/avatars/43/438f1b6217ee5600772cd1f9d978fbba999d8cc6.jpg</avatar>
        <avatarmedium>http://media.steampowered.com/steamcommunity/public/images/avatars/43/438f1b6217ee5600772cd1f9d978fbba999d8cc6_medium.jpg</avatarmedium>
        <avatarfull>http://media.steampowered.com/steamcommunity/public/images/avatars/43/438f1b6217ee5600772cd1f9d978fbba999d8cc6_full.jpg</avatarfull>
        <personastate>0</personastate>
        <primaryclanid>103582791431212131</primaryclanid>
        <timecreated>1203124452</timecreated>
        <personastateflags>0</personastateflags>
    </player>
  </players>
</response>

and I can output all of this to a web page with the following PHP code (code has come from another post, which was incredibly helpful

$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=[STEAMAPIKEY]&steamids=[PROFILEID]&format=xml";
$get = file_get_contents($url);
$xml = simplexml_load_string($get);

header('Content-Type: text/xml');
die($xml->asXML());

However when I attempt to change $xml->asXML() to another element like the documentation says to, like so:

echo '<p>'.$xml->response->players->player->personastate.'</p>;

I get an error saying that there is no data returned.

For the purpose of the example I am trying to retrieve the element <personastate> which indicates if the player is online or not.

도움이 되었습니까?

해결책

I'm pulling the solution you posted in the question out into this answer (and fixing a couple quoting errors).

As you noted, don't use the root element in the SimpleXML syntax:

INCORRECT echo '<p>'.$xml->response->players->player->personastate.'</p>';
CORRECT   echo '<p>'.$xml->players->player->personastate.'</p>';
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top