سؤال

I am trying to make a simple function that will take a submitted 64bit steamid and spit back info using the Steam API. I chose to use XML rather then JSON as i have used some XML in the past. I am still very new to php.

Getting the data and printing it works fine but i am having problems printing specific fields

$steamid = $_SESSION['steamid'];
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=X&steamids=$steamid&format=xml";
$data = file_get_contents($url);
$xml = simplexml_load_string($data);

print var_dump($xml);

//trying to display the "personaname" but doing it wrong
echo $xml->response->players->player->personaname;

Please note that removed my API key before posting here. This is an example of what i get returned using "print var_dump($xml)".

object(SimpleXMLElement)#1 (1) { ["players"]=> object(SimpleXMLElement)#2 (1) { ["player"]=> object(SimpleXMLElement)#3 (16) { ["steamid"]=> string(17) "76561198001776632" ["communityvisibilitystate"]=> string(1) "3" ["profilestate"]=> string(1) "1" ["personaname"]=> string(5) "johan" ["lastlogoff"]=> string(10) "1368300096" ["commentpermission"]=> string(1) "1" ["profileurl"]=> string(38) "http://steamcommunity.com/id/mrbbqlol/" ["avatar"]=> string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c.jpg" ["avatarmedium"]=> string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_medium.jpg" ["avatarfull"]=> string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_full.jpg" ["personastate"]=> string(1) "1" ["primaryclanid"]=> string(18) "103582791431859033" ["timecreated"]=> string(10) "1223663039" ["loccountrycode"]=> string(2) "SE" ["locstatecode"]=> string(2) "26" ["loccityid"]=> string(5) "43754" } } }

with the help of user hakre i printed out the raw XML, this is what it looks like:

<response>
<players>
<player>
<steamid>76561198001776632</steamid>
<communityvisibilitystate>3</communityvisibilitystate>
<profilestate>1</profilestate>
<personaname>johan</personaname>
<lastlogoff>1368300096</lastlogoff>
<commentpermission>1</commentpermission>
<profileurl>http://steamcommunity.com/id/mrbbqlol/</profileurl>
<avatar>
http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c.jpg
</avatar>
<avatarmedium>
http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_medium.jpg
</avatarmedium>
<avatarfull>
http://media.steampowered.com/steamcommunity/public/images/avatars/b7/b765fa514b9c44376e84754acb12e66821d4564c_full.jpg
</avatarfull>
<personastate>1</personastate>
<primaryclanid>103582791431859033</primaryclanid>
<timecreated>1223663039</timecreated>
<loccountrycode>SE</loccountrycode>
<locstatecode>26</locstatecode>
<loccityid>43754</loccityid>
</player>
</players>
</response>

I have looked at other similar questions but i have not been able to solve my problem. How should i parse and echo specific fields? Can simplexmlelement maybe be used for this?

Thanks

هل كانت مفيدة؟

المحلول

Can simplexmlelement maybe be used for this?

Yes it can and you already use it. However the problematic part is that you need to understand the XML structure first.

If you're debugging inside the browser the following is a quick help with simplexml:

$steamid = $_SESSION['steamid'];
$url     = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=X&steamids=$steamid&format=xml";
$data    = file_get_contents($url);
$xml     = simplexml_load_string($data);

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

The first section is actually exactly your code, I just added two lines below that out the XML verbatim to the browser telling it that it is XML. Your browwer will then show the XML as is (often nicely formatted and you can open/close parts, have pretty printed and syntax highlighted with different colors, for example in Chromium).

You the can access the elements as outlined in the basic SimpleXML usage examples. You can not trust print_r or var_dump on SimpleXMLElements much because they don't show you the whole XML (sub) tree.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top